0Pricing
Data Science Academy · Lesson

Rolling Windows and Lags

Smoothing and lag features.

Rolling Windows and Lags is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Smoothing Out the Bumps

Raw time series can be jumpy. A rolling window averages nearby points so the underlying signal becomes easy to see. 🌊

What a Window Really Is

A window is a fixed number of recent points. It slides forward one step at a time, recomputing a summary at each position.

The Rolling Mean

The most common move is a rolling mean: average the last few values to smooth the line. pandas makes it a one-liner.

sales.rolling(window=7).mean()

Window Size Is a Trade-Off

A larger window smooths more but reacts slowly; a smaller one stays responsive but noisy. You tune it to the question.

The First Values Are NaN

A window of 7 needs 7 points before it can compute. So the first rows come back as NaN until enough history exists.

Beyond the Mean

Rolling works with more than averages. A rolling std reveals how volatility changes over time, which trends alone hide.

sales.rolling(window=30).std()

Meet the Lag

A lag is simply the value from an earlier time step. Yesterday's sales as a column is a lag-1 feature.

Shift Creates Lags

pandas builds lags with shift. Calling shift(1) pushes every value down one row so each row sees its past.

df['lag_1'] = sales.shift(1)

Why Lags Power Forecasts

Most series depend on their own recent past. Feeding a model good lag features lets it learn that yesterday predicts today.

Differencing Is a Lag Too

Subtracting the previous value, value minus its lag, gives the change between steps. It is the seed of differencing.

df['change'] = sales - sales.shift(1)

Combine Rolling and Lags

Strong feature sets often mix both: a few lags plus a rolling mean. Together they capture recent level and direction.

Quick Check

Which pandas method shifts values to their previous time step to build a lag feature?

Recap: Past as Features

You used rolling windows to smooth and lags via shift to bring the past forward, the two staples of time series features.

Frequently asked questions

Is the “Rolling Windows and Lags” lesson free?

Yes — the full text of “Rolling Windows and Lags” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “Rolling Windows and Lags”?

Smoothing and lag features. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Rolling Windows and Lags” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Trend, Seasonality, and Noise
  2. Rolling Windows and Lags
  3. Stationarity and Differencing
  4. A First Forecast Baseline
← Back to Data Science Academy