I want to resample a pandas time series counting backwards. For example, let's set up a simple time series of 11 days:
>>> index = pd.date_range('01-01-2018', '01-11-2018', freq='D')
>>> randint = np.random.randint(low=0, high=9, size=(len(index), 1))
>>> df = pd.DataFrame(randint, index=index, columns=['random'])
>>> print(df)
random
2018-01-01 8
2018-01-02 8
2018-01-03 1
2018-01-04 4
2018-01-05 3
2018-01-06 5
2018-01-07 2
2018-01-08 6
2018-01-09 5
2018-01-10 1
2018-01-11 3
Default pandas behavior
If I resample it every 5 days, I'd get:
>>> df_5d = df.resample('5D').sum()
>>> print(df_5d)
random
2018-01-01 24
2018-01-06 19
2018-01-11 3
Basically you have 3 groupings: the first two groups have 5 members and the last group has 1, for a total of 11 members overall:
Start End
2018-01-01 2018-01-05
2018-01-06 2018-01-10
2018-01-11 2018-01-11
What I want is this
>>> df_5d = df.resample('5D').sum()
>>> print(df_5d)
random
2018-01-01 8
2018-01-02 21
2018-01-07 17
And the groupings are shown below. See how I counted '5D' backwards starting from the latest date:
Start End
2018-01-01 2018-01-01
2018-01-02 2018-01-06
2018-01-07 2018-01-11
How do I resample a pandas time series counting backwards?