Hi all so I have been playing around with rolling windows using .rolling() in pandas but it appears that the parameter 'center' only gives you the option to center your window or have it looking into the past. Is there a way to change this and have it look "forwards" without needing to reverse your data? Here is an example of a workaround I found where I reverse the data
series.iloc[::-1].rolling('2h', center=False)[::-1]
But I was wondering if anyone knew of a less hacky way to do this?
Suppose this Series
:
dti = pd.date_range('2022-02-22', periods=12, freq='H')
sr = pd.Series(range(len(dti)), index=dti)
print(sr)
# Output
2022-02-22 00:00:00 0
2022-02-22 01:00:00 1
2022-02-22 02:00:00 2
2022-02-22 03:00:00 3
2022-02-22 04:00:00 4
2022-02-22 05:00:00 5
2022-02-22 06:00:00 6
2022-02-22 07:00:00 7
2022-02-22 08:00:00 8
2022-02-22 09:00:00 9
2022-02-22 10:00:00 10
2022-02-22 11:00:00 11
Freq: H, dtype: int64
Use shift
:
>>> sr.shift(freq='-1H').rolling('2H').mean().reindex(sr.index)
2022-02-22 00:00:00 0.5
2022-02-22 01:00:00 1.5
2022-02-22 02:00:00 2.5
2022-02-22 03:00:00 3.5
2022-02-22 04:00:00 4.5
2022-02-22 05:00:00 5.5
2022-02-22 06:00:00 6.5
2022-02-22 07:00:00 7.5
2022-02-22 08:00:00 8.5
2022-02-22 09:00:00 9.5
2022-02-22 10:00:00 10.5
2022-02-22 11:00:00 NaN
Freq: H, dtype: float64