Pd.Grouper to shift the bin intervals

Viewed 86

In pandas 1.2 i would like to possibly adjust origin and offset to have the bins in the below example with exact yyyy-05-15 and yyyy-11-15 6M intervals. Here is the help link to pd.Grouper.

my attempt with the following code

import random
import pandas as pd
n_rows = 600
df = pd.DataFrame({'date': pd.date_range(periods=n_rows, end='2020-04-15'),
                   'current_start_date': '2020-11-15',
                   'current_end_date': '2021-05-15',
                    "a": range(n_rows)})
df[['current_start_date', 'current_end_date']] =  df[['current_start_date', 
                              'current_end_date']].apply(pd.to_datetime)

end = df[ 'current_end_date'].iloc[0]


df.groupby(pd.Grouper(freq='6M', key='date', origin=end, label='right', closed='right')).sum()

produces

Out[3]: 
                a
date             
2018-08-31     21
2019-02-28  17557
2019-08-31  51428
2020-02-29  84175
2020-08-31  26519
0 Answers
Related