Pandas count by each month in a date range

149 Views Asked by At

What I need is to calculate a number of actions by every month in the period from '2014-03' to '2019-01' for each module from start.

  month_range = ['2014-03', '2014-04', '2014-05', 
                 '2014-06', '2014-07', '2018-10', 
                 '2018-11', '2018-12', '2019-01']

  df = pd.DataFrame({'action':['done','in_work', 'done', 
                               'fail','in_work', 'done'], 
                     'module':['B1','B2','B1', 
                                'B3','B1','B2'],
                     'start': ['2014-06','2014-07','2014-07',
                               '2014-10','2018-09','2018-12'], 
                     'finish':['2014-06', NaT, '2018-10', 
                               '2014-10', NaT, '2019-02']
                          }, 
                     columns = ['action','module', 
                                'start', 'finish'])

A huge limitation. One action can be made from one month to another and be counted by every month in a date range.

For example, if module is still 'in work' & action has no ending (NaT) it should be counted by every month from the begining to max date in range.

Something in form like this is expected after calculating:

               B1 B2 B3
    1 2014-03  0   0  0
    2 2014-04  0   0  0
    3 2014-05  0   0  0
    4 2014-06  1   0  0
    5 2014-07  1   0  0

If someone knows any solution?

0

There are 0 best solutions below