Time Series Components and Stationarity
Trend, seasonality, noise, ADF test for stationarity, differencing to achieve stationarity.
Time Series Components and Stationarity is a free Learn AI with Python lesson on CoddyKit — lesson 1 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 Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Time Series?
A time series is data indexed in time order, such as daily sales or hourly temperature. Because observations are ordered and often correlated, special techniques are needed; you cannot just shuffle the rows like ordinary tabular data.
The Three Components
A classic view decomposes a series into:
- Trend: long-term upward or downward movement.
- Seasonality: repeating patterns at fixed periods (weekly, yearly).
- Residual: the leftover random noise after removing trend and seasonality.
Additive vs Multiplicative
In an additive model: y = trend + seasonality + residual, used when seasonal swings are constant in size.
In a multiplicative model: y = trend * seasonality * residual, used when seasonal swings grow with the trend.
seasonal_decompose
statsmodels can split a series into its components automatically.
from statsmodels.tsa.seasonal import seasonal_decompose
result = seasonal_decompose(series, model="additive", period=12)
result.trend
result.seasonal
result.residVisualizing the Decomposition
Plotting the result shows each component stacked, making trend and seasonality easy to see and helping you pick a forecasting approach.
import matplotlib.pyplot as plt
result.plot()
plt.tight_layout()
plt.show()What Is Stationarity?
A series is stationary when its statistical properties (mean, variance, autocorrelation) stay constant over time. Many classical models like ARIMA assume stationarity, so checking and enforcing it is a key first step.
Why Stationarity Matters
If the mean drifts or variance explodes over time, a model fit on early data will not generalize to later data. Removing trend and stabilizing variance makes the series predictable in a consistent way.
The ADF Test
The Augmented Dickey-Fuller (ADF) test checks for stationarity. The null hypothesis is "non-stationary (has a unit root)". A p-value below 0.05 lets you reject the null and treat the series as stationary.
from statsmodels.tsa.stattools import adfuller
stat, pvalue, *_ = adfuller(series)
print("p-value:", pvalue)
# pvalue < 0.05 -> stationaryDifferencing to Achieve Stationarity
Differencing subtracts each value from the previous one, removing trend. First-order differencing usually handles a linear trend; apply it again if needed.
diff1 = series.diff().dropna() # first difference
diff2 = series.diff().diff().dropna() # second differenceConfirming After Differencing
Re-run the ADF test on the differenced series. Once the p-value drops below 0.05 you have achieved stationarity and recorded the differencing order d, which feeds straight into ARIMA.
stat, pvalue, *_ = adfuller(series.diff().dropna())
print("p-value after differencing:", pvalue)Other Stabilizing Transforms
If variance grows over time, a log or Box-Cox transform stabilizes it before differencing. Combining a log transform with differencing handles many real-world multiplicative-growth series.
import numpy as np
log_series = np.log(series)
stationary = log_series.diff().dropna()Quick Check
Test your understanding of stationarity.
Recap
You decomposed series into trend, seasonality, and residual with seasonal_decompose, learned additive vs multiplicative models, tested stationarity with the ADF test, and used differencing (order d) to make a series stationary. Next: ARIMA and SARIMA.
Frequently asked questions
Is the “Time Series Components and Stationarity” lesson free?
Yes — the full text of “Time Series Components and Stationarity” is free to read here on the web, and the Learn AI with Python 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 Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Time Series Components and Stationarity”?
Trend, seasonality, noise, ADF test for stationarity, differencing to achieve stationarity. You practise Learn AI with Python 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 Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Time Series Components and Stationarity” 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 Learn AI with Python lesson?
Yes. Every Learn AI with Python 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
- Time Series Components and Stationarity
- ARIMA and SARIMA Models
- Prophet for Automated Forecasting
- LSTM for Time Series Forecasting