Rolling & Expanding Windows
A window function computes a statistic over a moving slice of nearby rows rather than the whole column at once — the engine behind moving averages, running totals, and smoothed trend lines in time-series data.
Learn Rolling & Expanding Windows in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You will master .rolling() for fixed-width windows, .expanding() for running totals, min_periods for the warm-up rows, and .ewm() for recency-weighted smoothing.
.rolling(window=3) creates a sliding window 3 rows wide. As it moves down the Series, it computes a statistic over each group of 3 consecutive rows. Pairing it with .mean() gives the classic 3-period moving average used to smooth noisy data.
Those leading NaN values appear because, by default, a window of 3 refuses to return a number until it has seen 3 values. The min_periods argument lowers that requirement: min_periods=1 says "give me a result as soon as there is at least one value", filling the early rows with partial-window answers.
.expanding() is a window that never forgets: row n uses every value from the start through n . .expanding().mean() is a running average, .expanding().sum() a running total. .ewm(span=...) is the exponentially weighted mean, where recent rows count for more and old rows fade out smoothly.
A rolling object alone is not data — it needs an aggregation:
Add three derived columns to a daily sales table.
Lesson complete — you can smooth and accumulate!
You can slide a .rolling() window, tame the warm-up rows with min_periods , accumulate with .expanding() , and weight the recent past with .ewm() .
🚀 Up next: GroupBy transform & filter — broadcast group stats back onto every row and keep or drop whole groups.
Practice quiz
What does s.rolling(window=3).mean() compute?
- A 3-period moving average
- The overall mean
- The cumulative sum
- The standard deviation of all rows
Answer: A 3-period moving average. It averages each sliding window of 3 consecutive rows.
Why are the first rows of a rolling(3) mean NaN?
- The data was unsorted
- A full window of 3 values is not yet available
- rolling drops them
- The mean is undefined
Answer: A full window of 3 values is not yet available. By default a full window is required before producing a value.
Which argument fills those warm-up rows with partial-window results?
- fillna=0
- warmup=True
- min_periods=1
- partial=True
Answer: min_periods=1. min_periods=1 computes as soon as one value is available.
What does .expanding() do compared with .rolling()?
- It uses a fixed 3-row window
- It weights recent rows more
- It only looks at one row
- It grows to include every row from the start to now
Answer: It grows to include every row from the start to now. An expanding window accumulates all prior rows (cumulative).
What does s.expanding().sum() give you?
- A running cumulative total
- The single grand total
- A 3-row moving sum
- The maximum so far
Answer: A running cumulative total. expanding().sum() produces a running total down the column.
What is special about .ewm()?
- It ignores recent values
- It weights recent observations more, older ones decay smoothly
- It requires a DatetimeIndex
- It returns integers
Answer: It weights recent observations more, older ones decay smoothly. Exponentially weighted means give recent data more influence.
Why is a bare s.rolling(3) not useful on its own?
- It raises an error
- It deletes rows
- It is a lazy Rolling object until you call an aggregator
- It returns the original Series
Answer: It is a lazy Rolling object until you call an aggregator. You must call .mean(), .sum(), etc. to get numbers.
How do you keep a rolling window from leaking across groups?
- Use min_periods=0
- Sort the index
- Drop NaN first
- key
- col
Answer: key. Grouping restarts the window within each group.
Which aggregators can you call on a rolling object?
- Only .mean()
- .mean(), .sum(), .max(), .std() and more
- Only .count()
- None — rolling has no aggregators
Answer: .mean(), .sum(), .max(), .std() and more. Rolling shares the usual aggregator methods like mean, sum, max, std.
What is the trade-off of using min_periods=1?
- It is always slower
- It removes the column
- Early results use tiny windows and are noisier
- It changes the dtype to object
Answer: Early results use tiny windows and are noisier. Filling early rows means those values come from small windows.