All lessons

Module 7: Time series · Lesson 7.4 · about 20 min

Growth rates: shift, pct_change and year over year

Compare each period with the one before and with the same period last year using shift and pct_change, compute growth per group, and avoid the common traps of missing periods and partial months.

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

Module 7 · about 20 minutes · uses Lesson 7.2. You’ll be able to compute period-over-period and year-over-year growth correctly.

Growth compares a period with an earlier one: shift moves a series down by some rows so you can divide this month by last month, or by the same month last year.

In everyday terms: laying this year’s calendar over last year’s, one month offset or twelve months offset, and comparing the boxes that line up.

1. shift and pct_change

Pythonorders.csv

Month-over-month swings mostly show the season (December up, January down). Year over year compares like with like, so it shows growth.

2. Per group

Pythonorders.csv

groupby(...).pct_change() restarts at each store, so each store is only compared with itself.

3. Traps

  • Missing periods: shift(12) means “12 rows back”. If a month is missing, it compares with the wrong month. Make the series regular first (Lesson 7.1).
  • Partial periods: comparing this month so far with all of last month looks like a fall. Compare the same number of days (month-to-date vs the same days last year).
  • Small bases: +300 % from 2 to 8 sales is noise. Show the counts beside the percentages.
  • Averaging growth rates: +50 % then −50 % is not 0; it’s −25 % overall. Compound growth uses the geometric mean.
shift(1)
Each row gets the previous row's value.
pct_change()
(this − previous) ÷ previous: growth as a share.
pct_change(12)
Year over year on monthly data.
groupby('store')[...].pct_change()
Growth within each store, never across stores.
Physical meaning
Growth is a ratio, so it depends on the base: from 10 to 20 is +100 %, from 20 to 10 is −50 %. Year over year compares like with like (December with December) and cancels the season.

“+50 % then −50 % gets you back where you started.” 100 → 150 → 75. Percentages compound on a changing base; work with the values, then compute the growth.

Watch it happen, one step at a time:

Monthly revenue.
monthrevenue
Jan200
Feb250
Mar225
shift(1).
monthrevenueprev
Jan200NaN
Feb250200
Mar225250
Divide. Feb: 250 ÷ 200 − 1 = +25 %. Mar: 225 ÷ 250 − 1 = −10 %.
One call. s.pct_change() gives NaN, 0.25, −0.10.
Per store. Sort by date first, then groupby('store')['revenue'].pct_change().

4. Your turn

Your turnorders.csv
How it works

Principle: growth compares a value with a reference value. shift(k) brings the reference onto the same row; then any formula works.

growtht=yt−yt−kyt−k,CAGR=(yendystart)1/years−1

With k = 1 it’s period over period; with k = the season length (12 months, 52 weeks, 4 quarters) it’s year over year.

5. Practice

  1. Week-over-week change in units (difference, not %).
  2. Sales went from $200k to $242k over 2 years. Compound annual growth?
  3. Why can shift(12) give the wrong comparison?
Answers
  1. weekly.diff() (= weekly - weekly.shift(1)).
  2. (242/200) ** 0.5 - 1 = 10 % a year.
  3. It counts rows, not months; a missing month shifts everything after it.

New words: shift, pct_change, diff, month over month, year over year, CAGR.

  • shift(n) lines a row up with the one n periods earlier.
  • pct_change() = this ÷ previous − 1.
  • Year over year on monthly data: pct_change(12).
  • Sort by date and group by store before shifting.

Check yourself

1What does s.shift(1) do?
2Monthly data: which gives year-over-year growth?
3Growth per store: what must you do so one store is not compared with another?

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.