Python Tutorial: Making time series stationary

Learn to test whether a time series is stationary using the Augmented Dickey-Fuller test and visual inspection. The tutorial covers interpreting the ADF test's p-value and test statistic, then shows how to transform non-stationary data into stationary data using differencing with pandas' diff method.
Chapters
Last time we learned about ways in which a time series can be non stationary and how we can identify it by plotting. However, there are more formal ways of accomplishing this task with statistical tests. There are also ways to transform non stationary time series into stationary ones. We'll address both of these in this lesson and then you'll be ready to start modeling. The most common test for identifying whether a time series is non stationary is the augmented Dickey Fuller test. This is a statistical test where the null hypothesis is that your time series is non stationary due to trend. We can implement the augmented Dickey Fuller test using STATS models. First we import the add Fuller function as shown. Then we can run it on our time series. The results object is a tuple. The zeroth element is the test statistic. In this case it is -1.34. The more negative this number is, the more likely that the data is stationary. The next item in the results tuple is the test P value. Here it is 0.6. If the P value is smaller than 0.05, we reject the null hypothesis and assume that our time series must be stationary. The last item in the tuple is a dictionary. This stores the critical values of the test statistic which equate to different p values. In this case, if we wanted a p value of 0.05 or below, our test statistic needed to be below minus 2.91. We will ignore the rest of the tuple items for now, but you can find out more about them here. Remember that it is always worth plotting your time series as well as doing the statistical tests. These tests are very useful, but sometimes they don't capture the full picture. Remember that the Dickey Fuller only tests for trend stationarity. In this example, although the time series behavior clearly changes and is non stationary, it passes the Dickey Fuller test. So let's say we have a time series that is non stationary. We need to transform the data into a stationary form before we can model it. You can think of this a bit like feature engineering in classic machine learning. Let's start with a non stationary dataset. Here is an example of the population of a city. One very common way to make a time series stationary is to take its difference. This is where from each value in our time series we subtract the previous value. We can do this using the diff method of a PANDAS dataframe. Notice that this gives us 1 nan value at the start. Since there is no previous value to subtract from it, we can get rid of this using the dropna method. Here is the time series. After differencing this time, taking the difference was enough to make it stationary. But for other time series we may need to take the difference more than once. Sometimes we will need to perform other transformations to make the time series stationary. This could be to take the log or the square root of a time series, or to calculate the proportional change. It can be hard to decide which of these to do, but often the simplest solution is the best one. You've learned how to test for stationarity and make time series stationary. Now.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







