I want to use rolling time windows in pandas for forward looking windows. How would I do that?
import pandas as pd
data = pd.DataFrame({'t': ['2017-02-02 15:00:01',
'2017-02-02 15:00:02',
'2017-02-02 15:01:00',
'2017-02-02 15:03:05',
'2017-02-02 15:08:00'],
'value': [1, 2, 3, 14, 5]})
data['t'] = data['t'].apply(pd.to_datetime)
data = data.set_index('t')
backward_max = data.rolling('300s').max() # yields [1, 2, 3, 14, 14]
forward_max = '???' # should yield [14, 14, 14, 14, 5]
Problem is that because of unequal time differences between observations, I cannot simply shift the result from the backward looking windows.
I could calculate fake time-stamps that go in reverse order and use backward time windows, as could be derived from here, but I am almost certain there is a more elegant way.
Edit: Changed all timestamps to the same day and provide the following workaround code, as inelegant as it is.
data2 = data.reset_index()
data2['t2'] = max(data2['t']) - (data2['t'] - min(data2['t']))
data2 = data2.set_index('t2').sort_index()
forward_max = np.flip(data2.rolling('300s')['value'].max().values, axis=0)