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.
Module 7 · about 20 minutes · uses Lesson 7.2. You’ll be able to compute period-over-period and year-over-year growth correctly.
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
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
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:
| month | revenue |
|---|---|
| Jan | 200 |
| Feb | 250 |
| Mar | 225 |
| month | revenue | prev |
|---|---|---|
| Jan | 200 | NaN |
| Feb | 250 | 200 |
| Mar | 225 | 250 |
s.pct_change() gives NaN, 0.25, −0.10.groupby('store')['revenue'].pct_change().4. Your turn
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.
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
- Week-over-week change in units (difference, not %).
- Sales went from $200k to $242k over 2 years. Compound annual growth?
- Why can
shift(12)give the wrong comparison?
Answers
weekly.diff()(=weekly - weekly.shift(1)).(242/200) ** 0.5 - 1= 10 % a year.- 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
Sources
- pandas documentation, User Guide: pandas.Series.pct_change https://pandas.pydata.org/docs/reference/api/pandas.Series.pct_change.html
- Hyndman, R. J. and Athanasopoulos, G. (2021), Forecasting: Principles and Practice, 3rd edition https://otexts.com/fpp3/
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.