I'm looking for a way to easily do a Pandas aggregation across a rolling window. For example, 90-day rolling daily, or 12-month rolling monthly.
If I want to calculate a monthly average, I can very easily do something like data.groupby([pd.Grouper(freq='M')]).agg({'value': 'mean', 'count': 'sum'})
.
However, in this case, I want to calculate the average over all observations in the preceding year period, reported monthly (or 30- or 90-day period, aggregated daily). I don't want to average the averages (e.g., from a resample), as some aggregations like geometric means would not be calculated correctly.
Is there a slick, idiomatic way of doing this? Everything I've done so far has been a few dozen lines of code duplicating a section of the data frame for each chunk, and I feel like there has to be something cleaner/faster.
Thanks!