Unit 9 • Lesson 5

Rolling Windows

Overview

Smoothing a noisy series to see the shape underneath.

Intermediate 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will be able to:

  • Take a moving average over a window.
  • Say why the first values are missing.
  • Choose a window length deliberately.
  • State the smoothing when you report the result.

Why This Matters

Daily numbers are noisy enough that the shape is invisible; a seven-day mean makes it legible. The trade is real, though: you are showing something that never happened, so the window length is a choice you have to declare rather than tune until the chart looks good.

Step 1: A moving average

rolling takes a window and a summary. A seven-day mean of daily data is the standard way to make a weekly pattern stop drowning the trend.

1

The call

Each output value is the summary of that row and the ones before it, within the window.

import pandas as pd

s = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])
print(s.rolling(3).mean().tolist())
2

Choosing the window

A window of 7 on daily data removes the day-of-week pattern because every window contains one of each day. That is a reason, and reasons are what a window length should have.

import pandas as pd

s = pd.Series([10.0, 2.0, 11.0, 3.0, 12.0, 4.0, 13.0, 5.0])
print([round(x, 1) for x in s.rolling(2).mean().dropna().tolist()])
print([round(x, 1) for x in s.rolling(4).mean().dropna().tolist()])

The wider the window, the smoother the line and the later it reacts to a real change. Smoothing is a trade between legibility and responsiveness, not a free improvement.

Mini Practice #1: Two window lengths

Try It Yourself

Smooth this noisy series with a window of 2 and then 4, and see which one shows the trend.

Press Run to see output

Step 2: The first values have no window

A window of three has nothing to average until the third value, so the first two are NaN. This is correct, and it is why a smoothed series is shorter than the one it came from.

1

The gap at the front

Not an error — there genuinely is no three-day average on day one.

import pandas as pd

s = pd.Series([1.0, 2.0, 3.0, 4.0])
print(s.rolling(3).mean().tolist())
print(s.rolling(3).mean().dropna().tolist())
2

min_periods, and what it costs

It fills the front by averaging over fewer points. That is defensible for a chart and dishonest in a table, because the early values are computed differently from the rest.

import pandas as pd

s = pd.Series([1.0, 2.0, 3.0, 4.0])
print(s.rolling(3, min_periods=1).mean().tolist())

Mini Practice #2: The gap at the front

Try It Yourself

Print the rolling mean with and without the leading gaps, and count how many were dropped.

Press Run to see output

Step 3: When to Use It

Smoothing a noisy series

A 7-day rolling mean shows the trend under daily ups and downs.

df["sales"].rolling(7).mean()

Recent-window checks

A rolling max or sum over the last n rows for alerts and running totals.

When Not to Use It

Do not forget the first window-minus-one rows are NaN, and do not use a row-count window on data with gaps. Use a time window like "7D" instead.

End-of-Lesson Exercises

Exercise 1: Smooth a series

Write smoothed(s, window) returning the rolling mean as a list, with the leading gaps dropped.

s.rolling(window).mean() gives the smoothed series with gaps at the front; dropna() removes them and tolist() hands back plain floats.

Press Run to try it, then Check my work to mark it.

Exercise 2: What the smoothing cost

Write smoothing_cost(s, window) returning a two-element list of [values in, values out after dropping the leading gaps]. The difference is how much of the start you gave up, and it is worth stating alongside a smoothed chart.

len(s) for the first, then the length of s.rolling(window).mean().dropna() for the second. The gap is window - 1 whenever the series is long enough.

Press Run to try it, then Check my work to mark it.