0
0
MlopsHow-ToBeginner · 3 min read

How to Check Stationarity in Time Series in Python

To check stationarity in a time series in Python, use the adfuller function from the statsmodels library, which performs the Augmented Dickey-Fuller test. This test returns a p-value; if it is less than 0.05, the series is considered stationary.
📐

Syntax

The main function to check stationarity is adfuller from statsmodels.tsa.stattools. It takes a time series array as input and returns several values:

  • test statistic: value to compare with critical values
  • p-value: probability to reject stationarity
  • used lag: number of lags used in test
  • number of observations: data points used
  • critical values: thresholds for different confidence levels

Use the p-value to decide if the series is stationary (p < 0.05 means stationary).

python
from statsmodels.tsa.stattools import adfuller

def check_stationarity(timeseries):
    result = adfuller(timeseries)
    test_statistic = result[0]
    p_value = result[1]
    used_lag = result[2]
    n_obs = result[3]
    critical_values = result[4]
    return test_statistic, p_value, used_lag, n_obs, critical_values
💻

Example

This example shows how to check if a generated random walk time series is stationary or not using the Augmented Dickey-Fuller test.

python
import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import adfuller

# Generate a random walk (non-stationary series)
np.random.seed(42)
random_walk = np.cumsum(np.random.normal(loc=0, scale=1, size=100))

# Perform ADF test
result = adfuller(random_walk)

print(f"Test Statistic: {result[0]:.4f}")
print(f"p-value: {result[1]:.4f}")
print("Critical Values:")
for key, value in result[4].items():
    print(f"   {key}: {value:.4f}")

# Interpretation
if result[1] < 0.05:
    print("The series is stationary.")
else:
    print("The series is non-stationary.")
Output
Test Statistic: -1.3633 p-value: 0.6023 Critical Values: 1%: -3.4981 5%: -2.8912 10%: -2.5828 The series is non-stationary.
⚠️

Common Pitfalls

  • Ignoring p-value: Always check the p-value, not just the test statistic.
  • Wrong data format: Input must be a 1D numeric array or list.
  • Not differencing non-stationary data: If non-stationary, apply differencing before modeling.
  • Using small sample sizes: Small data can give unreliable test results.
python
import numpy as np
from statsmodels.tsa.stattools import adfuller

# Wrong: Passing 2D array
try:
    data = np.random.normal(size=(10,2))
    adfuller(data)
except Exception as e:
    print(f"Error: {e}")

# Right: Pass 1D array
data_1d = np.random.normal(size=10)
result = adfuller(data_1d)
print(f"p-value: {result[1]:.4f}")
Output
Error: x must be 1-dimensional p-value: 0.0000
📊

Quick Reference

Use the following quick tips when checking stationarity:

  • Use adfuller from statsmodels for the Augmented Dickey-Fuller test.
  • Interpret p-value: < 0.05 means stationary, otherwise non-stationary.
  • Apply differencing or transformations if series is non-stationary.
  • Ensure data is 1D numeric array or list.

Key Takeaways

Use the Augmented Dickey-Fuller test via statsmodels.adfuller to check stationarity.
A p-value less than 0.05 indicates the time series is stationary.
Input data must be a one-dimensional numeric array or list.
If non-stationary, apply differencing or other transformations before modeling.
Small sample sizes can lead to unreliable stationarity test results.