'Pandas: How to make a rolling window computation output the PeriodIndex of the window instead of the DatetimeIndex of the window's initial timestamp

I have a pd.DataFrame containing a time-series: the index is a pd.DatetimeIndex with dtype=datetime64[ns].

timestamp                  my_column
2022-01-24 23:00:07.620    407.081879
2022-01-24 23:00:17.619    405.707489
2022-01-24 23:00:27.622    407.081879
2022-01-24 23:00:37.614    406.720184
2022-01-24 23:00:47.611    407.081879
2022-01-24 23:00:57.616    407.407410
2022-01-24 23:01:07.611    406.756378
2022-01-24 23:01:17.622    406.430847
2022-01-24 23:01:27.623    407.407410
2022-01-24 23:01:37.622    406.430847
2022-01-24 23:01:47.611    407.081879
2022-01-24 23:01:57.623    406.430847
2022-01-24 23:02:07.625    406.756378
2022-01-24 23:02:17.612    405.707489
2022-01-24 23:02:27.609    405.381958
2022-01-24 23:02:37.617    406.430847
2022-01-24 23:02:47.612    405.743622
2022-01-24 23:02:57.615    406.756378
2022-01-24 23:03:07.621    407.081879
2022-01-24 23:03:17.621    406.032990
...

I'd like to compute the median on rolling windows where the output index is the period of that window.

I'm able to do it with pd.DataFrame.resample, because it has the parameter kind='period', with the following line (but resample gives no overlapping and I want it)

downsampled_median = df['my_column'].resample('.5H', kind='period').median()

but pd.DataFrame.rolling does not have the kind parameter and

windowed_median = df['my_column'].rolling('.5H').median()

outputs

2022-01-24 23:00:07.620         NaN
2022-01-24 23:00:17.619   -0.217041
2022-01-24 23:00:27.622    0.000000
2022-01-24 23:00:37.614    0.000000
2022-01-24 23:00:47.611   -0.054260
2022-01-24 23:00:57.616    0.000000
2022-01-24 23:01:07.611    0.000000
2022-01-24 23:01:17.622    0.000000
2022-01-24 23:01:27.623    0.000000
2022-01-24 23:01:37.622    0.000000
2022-01-24 23:01:47.611    0.010852
2022-01-24 23:01:57.623    0.000000
2022-01-24 23:02:07.625    0.000000
2022-01-24 23:02:17.612    0.000000
2022-01-24 23:02:27.609    0.000000
2022-01-24 23:02:37.617    0.000000
2022-01-24 23:02:47.612    0.000000
2022-01-24 23:02:57.615    0.000000
2022-01-24 23:03:07.621    0.000000
2022-01-24 23:03:17.621    0.000000
...

where the index is again a DatetimeIndex of dtype='datetime64[ns]' and I'm struggling to convert it to an appropriate pd.PeriodIndex with the start_time equal to the first timestamp of that window and end_time equal to the last timestamp of that window.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source