How to check if Time Series Data is Stationary with Python?

Time series data is a collection of data points recorded at regular intervals. To make accurate forecasts, it's essential to check if the data is stationary - meaning its statistical properties don't change over time. Python provides several methods to test stationarity.

What is Stationarity?

A time series is stationary if its mean, variance, and autocorrelation remain constant over time. Non-stationary data shows trends, seasonality, or changing variance that can mislead forecasting models.

Augmented Dickey-Fuller (ADF) Test

The ADF test checks for unit roots in time series data. It tests the null hypothesis that the data is non-stationary. If the p-value is less than 0.05, we reject the null hypothesis and conclude the data is stationary.

Example

Let's perform an ADF test using the adfuller() function from statsmodels ?

from statsmodels.tsa.stattools import adfuller
import pandas as pd

# Load sample time series data
data = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', 
                   parse_dates=['date'], index_col='date')
series = data['value']

# Perform ADF test
result = adfuller(series)

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

# Interpret results
if result[1] <= 0.05:
    print("Data is stationary (reject null hypothesis)")
else:
    print("Data is non-stationary (fail to reject null hypothesis)")
ADF Statistic: 3.145185689306744
p-value: 1.0
Critical Values:
	1%: -3.465620397124192
	5%: -2.8770397560752436
	10%: -2.5750324547306476
Data is non-stationary (fail to reject null hypothesis)

KPSS Test

The KPSS (Kwiatkowski-Phillips-Schmidt-Shin) test has the opposite null hypothesis - it assumes data is stationary. If p-value

Example

Performing KPSS test on the same dataset ?

from statsmodels.tsa.stattools import kpss
import pandas as pd

# Load data
data = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', 
                   parse_dates=['date'], index_col='date')
series = data['value']

# Perform KPSS test
result = kpss(series, regression='c')

print('KPSS Statistic:', result[0])
print('p-value:', result[1])
print('Critical Values:')
for key, value in result[3].items():
    print(f'\t{key}: {value}')

# Interpret results
if result[1] <= 0.05:
    print("Data is non-stationary (reject null hypothesis)")
else:
    print("Data is stationary (fail to reject null hypothesis)")
KPSS Statistic: 2.0131256386303322
p-value: 0.01
Critical Values:
	10%: 0.347
	5%: 0.463
	2.5%: 0.574
	1%: 0.739
Data is non-stationary (reject null hypothesis)

Visual Analysis with Rolling Statistics

You can also check stationarity visually by plotting rolling mean and standard deviation. If they remain relatively constant, the data is likely stationary.

Example

Creating a visual test using rolling statistics ?

import pandas as pd
import matplotlib.pyplot as plt

# Load data
data = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', 
                   parse_dates=['date'], index_col='date')

# Calculate rolling statistics
window = 12
rolling_mean = data['value'].rolling(window=window).mean()
rolling_std = data['value'].rolling(window=window).std()

# Create plot
plt.figure(figsize=(12, 8))
plt.plot(data['value'], color='blue', label='Original Data')
plt.plot(rolling_mean, color='red', label=f'Rolling Mean ({window} months)')
plt.plot(rolling_std, color='black', label=f'Rolling Std ({window} months)')
plt.legend(loc='best')
plt.title('Rolling Statistics - Stationarity Check')
plt.xlabel('Date')
plt.ylabel('Value')
plt.show()

Comparison of Methods

Method Null Hypothesis Stationary if Best For
ADF Test Data is non-stationary p-value < 0.05 Detecting unit roots
KPSS Test Data is stationary p-value > 0.05 Confirming stationarity
Rolling Statistics Visual inspection Constant mean/variance Quick visual check

Conclusion

Use ADF and KPSS tests together for robust stationarity testing - if ADF suggests stationarity and KPSS confirms it, you can be confident in your results. Visual inspection with rolling statistics provides additional insight into the data's behavior over time.

Updated on: 2026-03-27T11:34:28+05:30

594 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements