Stationarity and Differencing
Preparing a series for modeling.
Stationarity and Differencing is a free Data Science Academy lesson on CoddyKit — lesson 3 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.
Models Like Steady Data
Most forecasting models assume the series behaves consistently over time. That property has a name: stationarity. 🎯
What Stationary Means
A stationary series has a stable mean and spread that do not drift over time. Its statistical mood stays the same.
Trends Break Stationarity
A rising trend means the mean keeps climbing, so the series is non-stationary. Many models will struggle until you fix that.
Seasonality Breaks It Too
Repeating seasonal swings make the average rise and fall on a cycle. That changing pattern also counts as non-stationary.
The ADF Test
You do not have to guess. The ADF test checks stationarity and returns a p-value you can read.
from statsmodels.tsa.stattools import adfuller
result = adfuller(sales)Reading the P-Value
If the ADF p-value is below 0.05, the series is likely stationary. Above it, you probably still have a trend to remove.
Differencing to the Rescue
Differencing replaces each value with how much it changed from the last step. This simple move often erases a trend.
diffed = sales.diff()Why It Removes Trends
By looking at changes instead of levels, a steady climb becomes a flat line wobbling around zero, which is stationary.
Drop the First NaN
The very first row has nothing to subtract, so diff leaves it NaN. Drop it before testing or modeling.
diffed = sales.diff().dropna()Differencing Again
If one round is not enough, difference the result a second time. The number of rounds becomes the d term in ARIMA.
Seasonal Differencing
To kill a yearly cycle, subtract the value 12 steps back. That seasonal diff targets the repeating pattern directly.
seasonal = sales.diff(12)Quick Check
An ADF test on your raw series returns a p-value of 0.40. What does that suggest?
Recap: Make It Steady
You learned to test stationarity with ADF and to remove trends and cycles using differencing before modeling.
Frequently asked questions
Is the “Stationarity and Differencing” lesson free?
Yes — the full text of “Stationarity and Differencing” 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 “Stationarity and Differencing”?
Preparing a series for modeling. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Stationarity and Differencing” 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
- Trend, Seasonality, and Noise
- Rolling Windows and Lags
- Stationarity and Differencing
- A First Forecast Baseline