When creating a pandas DataFrame with a daily PeriodIndex, and doing a rolling window calculation based on an offset (e.g. '5D') the result is not as expected.
I expect the rolling window to be the last 5 days, but instead the window seems to be from the start of the index, and growing for each item.
The rolling works as expected for a DataFrame with a DatetimeIndex
df_datetimeindex = pd.DataFrame([0.5]*10, columns=['data'], index=pd.date_range(start='2019', periods=10, freq='D'))
df_periodindex = pd.DataFrame([0.5]*10, columns=['data'], index=pd.period_range(start='2019', periods=10, freq='D'))
display(
df_datetimeindex.data.rolling('5D', min_periods=1).sum(),
df_periodindex.data.rolling('5D', min_periods=1).sum()
)
display(df_datetimeindex.index, df_periodindex.index)
display(df_datetimeindex.data.rolling('5D', min_periods=1), df_periodindex.data.rolling('5D', min_periods=1))
yields
2019-01-01 0.5
2019-01-02 1.0
2019-01-03 1.5
2019-01-04 2.0
2019-01-05 2.5
2019-01-06 2.5
2019-01-07 2.5
2019-01-08 2.5
2019-01-09 2.5
2019-01-10 2.5
Freq: D, Name: data, dtype: float64
2019-01-01 0.5
2019-01-02 1.0
2019-01-03 1.5
2019-01-04 2.0
2019-01-05 2.5
2019-01-06 3.0
2019-01-07 3.5
2019-01-08 4.0
2019-01-09 4.5
2019-01-10 5.0
Freq: D, Name: data, dtype: float64
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09', '2019-01-10'],
dtype='datetime64[ns]', freq='D')
PeriodIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09', '2019-01-10'],
dtype='period[D]', freq='D')
Rolling [window=432000000000000,min_periods=1,center=False,win_type=freq,axis=0]
Rolling [window=432000000000000,min_periods=1,center=False,win_type=freq,axis=0]
Is there an explanation for this behavior that I have missed?