Skip to content Skip to sidebar Skip to footer

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()

graph

Explanation:

  1. First create Series from list of dates and convert to_datetimes.
  2. Create PeriodIndex by Series.dt.to_period
  3. groupby by index (level=0) and get counts by GroupBy.size
  4. Add missing periods by Series.reindex by PeriodIndex created by max and min values of index
  5. 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"