A rolling window looks at the last n rows for each row:
lake["revenue"].rolling(7).mean() # 7-day moving average
- The first 6 values are
NaN, because there aren't 7 rows yet. min_periods=1 computes with whatever rows are available.
rolling(7) counts rows. If days are missing (a store closed on Mondays), 7 rows span more than 7 days. With a DatetimeIndex, rolling("7D") counts calendar days instead.
center=True puts the window around the row rather than behind it, which suits smoothing a chart but not a figure you report as of that day.
- Any aggregation works:
.sum(), .max(), .median().
Rolling averages are how dashboards show a trend without the weekday zigzag.