All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 7 · about 15 minutes · uses Lesson 7.1. You’ll be able to smooth daily KPIs and compute running totals.

A rolling average replaces each day by the average of the last few days, so day-to-day noise cancels and the trend shows.

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

Pythonorders.csv

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

Pythonorders.csv

expanding() is a window that grows from the start: running totals, running maximums. .cumsum() does the same for sums.

3. Exponential smoothing

Pythonorders.csv

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:

Daily sales.
daysales
110
214
39
415
512
Window of 3. s.rolling(3).mean(); days 1-2 have no full window: NaN.
Day 3. (10 + 14 + 9) ÷ 3 = 11.
Slide. Day 4: (14 + 9 + 15) ÷ 3 = 12.67; day 5: (9 + 15 + 12) ÷ 3 = 12.
Result.
daysalesroll3
110NaN
214NaN
3911.0
41512.67
51212.0

4. Your turn

Your turnorders.csv
How it works

Principle: a rolling mean of k points averages away noise (its SD shrinks by about 1/k) 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:

y‾t=1k∑j=0k−1yt−j

5. Practice

  1. A 28-day rolling sum of revenue.
  2. Why does a trailing 7-day average turn late after a big change?
  3. Running maximum of daily sales.
Answers
  1. daily.rolling(28).sum() (or rolling('28D')).
  2. 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.
  3. daily.cummax() or daily.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

1Why use a 7-day rolling average on daily sales?
2What are the first 6 values of rolling(7).mean()?
3What does rolling('7D') do differently from rolling(7)?

Sources

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.