Now Reading
How To Check Time-Series Stationarity? A Beginners Guide in Python

How To Check Time-Series Stationarity? A Beginners Guide in Python

Time series

Whether you want to predict the temperature of the environment or estimate electricity consumption for the next few months, stationarity of time series models is necessary if you want the forecasting model to work well. Most of the time series models are non-stationary either it gives an upward or downward trend or seasonal effects. 

The main aim of this article is to discuss the methods for checking the stationarity in time series data. We will do the experiments on the time series data to check this.

How To Start Your Career In Data Science?


In the above plot we observe that data do not change over time. It won’t show any trend or seasonal effect. Mean and variance remains constant over time.



Plot show data gives a trend or seasonal effect with respect to time. Mean and variance change over time.

About the Dataset

The dataset is taken from Kaggle, you can find it here. It has information about time and electric production. It has 2 columns(time and value) and 397 rows.

Code Implementation

The code is implemented in Google Colab and .pynb file download to local device.

# Mounting drive
from google.colab import drive
#Path to directory
import os
os.chdir('/content/drive/My Drive/AIM')

First step is to mount the drive and set the path of the directory.

#Importing Train Dataset
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.tsa.stattools import adfuller
train = pd.read_csv("Electric_Production.csv")

Procedure to Checking Stationarity

1.Visual Test

#Plot the Graph

Looking at the plot we can observe there is an upward trend over the period of time.

#Plot the Histogram

The plot shows a slightly skewed distribution. The histogram doesn’t show normal distribution over a period of time.

From the above plots, we can conclude the time series data is non-stationary.

  1. Summary Statistics
#Split the data

We will proceed by splitting the data into two parts so that we can then check the mean and variance of the data.

See Also

#Mean of data
#Variance of data

Looking at the above results the mean and variance of the first part is very different from the second part. It shows another indication of non-stationary time series data.

  1. Augmented Dickey-Fuller Test

Augmented Dickey-Fuller Test is a common statistical test used to test whether a given Time series is stationary or not. We can achieve this by defining the null and alternate hypothesis.

  • Null Hypothesis: Time Series is stationary. It gives a time-dependent trend.
  • Alternate Hypothesis: Time Series is non-stationary. In another term, the series doesn’t depend on time.
  • ADF or t Statistic < critical values: Accept the null hypothesis. Time series is stationary.
  • ADF or t Statistic > critical values: Failed to reject the null hypothesis. The time series is non-stationary
#ADF statistic to check stationarity
t = train["Value"].values
timeseries = adfuller(t)
print('ADF Statistic: %f' % result[0])
print('p-value: %f' % result[1])
print('Critical Values:')
for key, value in timeseries[4].items():
 print('\t%s: %.3f' % (key, value))
if timeseries[0] > timeseries[4]["5%"]:
print ("Failed to Reject Ho - Time Series is Non-Stationary")
 print ("Reject Ho - Time Series is Stationary")

From the above result, we observe ADF statistic is greater than critical values. So we fail to reject the null hypothesis. Time series is non-stationary.


In this article, we have applied different techniques to check whether the time series is stationary or not. As most of the time-series data show a trend which is critical for the performance of the forecasting models. So we can expand our future research to make the time series stationary.

The complete code of the above implementation is available at the AIM’s GitHub repository. Please visit this link to find the notebook of this code.

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.
Join our Telegram Group. Be part of an engaging community

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top