Python - Group Dates By Month
Here's a quick problem that I, at first, dismissed as easy. An hour in, and I'm not so sure! So, I have a list of Python datetime objects, and I want to graph them. The x-values ar
Solution 1:
I suggest use pandas
:
import pandas as pd
dates = ['28/02/2018', '01/03/2018', '16/03/2018', '17/05/2018']
s = pd.to_datetime(pd.Series(dates), format='%d/%m/%Y')
s.index = s.dt.to_period('m')
s = s.groupby(level=0).size()
s = s.reindex(pd.period_range(s.index.min(), s.index.max(), freq='m'), fill_value=0)
print (s)
2018-02 12018-03 22018-04 02018-05 1
Freq: M, dtype: int64
s.plot.bar()
Explanation:
- First create
Series
from list ofdate
s and convertto_datetime
s. - Create
PeriodIndex
bySeries.dt.to_period
groupby
by index (level=0
) and get counts byGroupBy.size
- Add missing periods by
Series.reindex
byPeriodIndex
created by max and min values of index - Last plot, e.g. for bars -
Series.plot.bar
Solution 2:
using Counter
dates = list()
import random
import collections
for y in range(2015,2019):
for m in range(1,13):
for i in range(random.randint(1,4)):
dates.append("{}/{}".format(m,y))
print(dates)
counter = collections.Counter(dates)
print(counter)
for your problem with dates with no occurrences you can use the subtract
method of Counter
generate a list with all range of dates, each date will appear on the list only once, and then you can use subtract
like so
tmp_date_list = ["{}/{}".format(m,y) for y inrange(2015,2019) for m inrange(1,13)]
counter.subtract(tmp_date_list)
Post a Comment for "Python - Group Dates By Month"