Module 7: Time series · Lesson 7.3 · about 15 min
Rolling averages
Smooth a noisy daily series with rolling windows: 7-day and 28-day averages, rolling sums, time-based windows, centred windows for history, expanding totals, and exponentially weighted averages.
Module 7 · about 15 minutes · uses Lesson 7.1. You’ll be able to smooth daily KPIs and compute running totals.
In everyday terms: judging a café by its last seven days instead of today alone: one rainy Tuesday no longer looks like a disaster.
1. rolling
Each value is the mean of the current day and the 6 before it (a trailing window), so it lags the real change by about half a window. For charts of history, rolling(7, center=True) doesn’t lag, but it needs future days, so it can’t be used for the latest days or forecasts.
2. Windows by time, and running totals
expanding() is a window that grows from the start: running totals, running maximums. .cumsum() does the same for sums.
3. Exponential smoothing
ewm weights recent days more (Statistics Lesson 8.5). alpha near 1 follows the data closely; near 0 is very smooth.
- rolling(7).mean()
- Average of this row and the 6 before it.
- First rows NaN
- Until 7 rows exist; min_periods= relaxes that.
- rolling('7D')
- A window by time, safe when days are missing.
- cumsum() / ewm(span=7)
- Running total; a smoother that weights recent days more.
- Physical meaning
- A 7-day window covers each weekday exactly once, so weekly patterns cancel and only the trend remains. The price: the average lags, responding to a real change a few days late.
“A rolling average shows today’s value.” It shows the last week’s average, centred about 3 days back. A sudden drop takes a week to show fully.
Watch it happen, one step at a time:
| day | sales |
|---|---|
| 1 | 10 |
| 2 | 14 |
| 3 | 9 |
| 4 | 15 |
| 5 | 12 |
s.rolling(3).mean(); days 1-2 have no full window: NaN.| day | sales | roll3 |
|---|---|---|
| 1 | 10 | NaN |
| 2 | 14 | NaN |
| 3 | 9 | 11.0 |
| 4 | 15 | 12.67 |
| 5 | 12 | 12.0 |
4. Your turn
How it works
Principle: a rolling mean of k points averages away noise (its SD shrinks by about ) at the cost of lag and lost detail. Choose k from the pattern you want to remove: 7 for a weekly cycle, 28 or 30 for a monthly view.
Trailing mean at day t:
5. Practice
- A 28-day rolling sum of revenue.
- Why does a trailing 7-day average turn late after a big change?
- Running maximum of daily sales.
Answers
daily.rolling(28).sum()(orrolling('28D')).- On the day of the change only 1 of the 7 values reflects it; it takes a week for the window to fill with new values.
daily.cummax()ordaily.expanding().max().
Try it on your own data: time series splits a series into trend and seasonal pattern.
New words: rolling window, trailing, centred, expanding, year to date, ewm.
- rolling(n).mean() averages the current row and the n − 1 before it.
- The first n − 1 rows are NaN unless you set min_periods.
- Use a 7-day window for daily data with a weekly pattern.
- cumsum() for running totals; ewm() for recent-weighted smoothing.
Check yourself
Sources
- pandas documentation, User Guide: Windowing operations https://pandas.pydata.org/docs/user_guide/window.html
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.