How to Difference a Time Series Dataset with Python

Differencing is a popular and widely used data transform for time series.

In this tutorial, you will discover how to apply the difference operation to your time series data with Python.

After completing this tutorial, you will know:

  • About the differencing operation, including the configuration of the lag difference and the difference order.
  • How to develop a manual implementation of the differencing operation.
  • How to use the built-in Pandas differencing function.

Let’s get started.

How to Difference a Time Series Dataset with Python

How to Difference a Time Series Dataset with Python
Photo by Marcus, some rights reserved.

Why Difference Time Series Data?

Differencing is a method of transforming a time series dataset.

It can be used to remove the series dependence on time, so-called temporal dependence. This includes structures like trends and seasonality.

Differencing can help stabilize the mean of the time series by removing changes in the level of a time series, and so eliminating (or reducing) trend and seasonality.

— Page 215, Forecasting: principles and practice

Differencing is performed by subtracting the previous observation from the current observation.

In this way, a series of differences can be calculated.

Lag Difference

Taking the difference between consecutive observations is called a lag-1 difference.

The lag difference can be adjusted to suit the specific temporal structure.

For time series with a seasonal component, the lag may be expected to be the period (width) of the seasonality.

Difference Order

Temporal structure may still exist after performing a differencing operation, such as in the case of a nonlinear trend.

As such, the process of differencing can be repeated more than once until all temporal dependence has been removed.

The number of times that differencing is performed is called the difference order.

Stop learning Time Series Forecasting the slow way!

Take my free 7-day email course and discover data prep, modeling and more (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Start Your FREE Mini-Course Now!

Shampoo Sales Dataset

This dataset describes the monthly number of sales of shampoo over a 3 year period.

The units are a sales count and there are 36 observations. The original dataset is credited to Makridakis, Wheelwright, and Hyndman (1998).

You can download and learn more about the dataset here.

The example below loads and creates a plot of the loaded dataset.

Running the example creates the plot that shows a clear linear trend in the data.

Shampoo Sales Dataset Plot

Shampoo Sales Dataset Plot

Manual Differencing

We can difference the dataset manually.

This involves developing a new function that creates a differenced dataset. The function would loop through a provided series and calculate the differenced values at the specified interval or lag.

The function below named difference() implements this procedure.

We can see that the function is careful to begin the differenced dataset after the specified interval to ensure differenced values can, in fact, be calculated. A default interval or lag value of 1 is defined. This is a sensible default.

One further improvement would be to also be able to specify the order or number of times to perform the differencing operation.

The example below applies the manual difference() function to the Shampoo Sales dataset.

Running the example creates the differenced dataset and plots the result.

Manually Differenced Shampoo Sales Dataset

Manually Differenced Shampoo Sales Dataset

Automatic Differencing

The Pandas library provides a function to automatically calculate the difference of a dataset.

This diff() function is provided on both the Series and DataFrame objects.

Like the manually defined difference function in the previous section, it takes an argument to specify the interval or lag, in this case called the periods.

The example below demonstrates how to use the built-in difference function on the Pandas Series object.

As in the previous section, running the example plots the differenced dataset.

A benefit of using the Pandas function, in addition to requiring less code, is that it maintains the date-time information for the differenced series.

Automatic Differenced Shampoo Sales Dataset

Automatic Differenced Shampoo Sales Dataset


In this tutorial, you discovered how to apply the difference operation to time series data with Python.

Specifically, you learned:

  • About the difference operation, including the configuration of lag and order.
  • How to implement the difference transform manually.
  • How to use the built-in Pandas implementation of the difference transform.

Do you have any questions about differencing, or about this post?
Ask your questions in the comments below.

Want to Develop Time Series Forecasts with Python?

Introduction to Time Series Forecasting With Python

Develop Your Own Forecasts in Minutes

...with just a few lines of python code

Discover how in my new Ebook:
Introduction to Time Series Forecasting With Python

It covers self-study tutorials and end-to-end projects on topics like:
Loading data, visualization, modeling, algorithm tuning, and much more...

Finally Bring Time Series Forecasting to
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.

16 Responses to How to Difference a Time Series Dataset with Python

  1. Srinath Jayachandran February 26, 2017 at 3:58 pm #

    Hi there, here is a recent work on time series that gives a time series a symbolic representation.

  2. NL May 3, 2017 at 1:54 am #

    Have a question. What if the difference is negative?

    • Jason Brownlee May 3, 2017 at 7:39 am #

      Some differences will be positive, some negative.

      • Manuel Jose Alvarez December 4, 2017 at 12:06 pm #

        Hi, which will be the most pythonic way to set the negative difeferece as zero. Let say that I have some bookings for t+1 and a forecast.

        • Jason Brownlee December 4, 2017 at 4:58 pm #

          My approach is make it work first, then make it readable.

  3. Hans June 16, 2017 at 1:06 pm #

    Are difference functions only useful to remove structures like trends and seasonality,
    or can they also be used to build features from trends in data sets?

    What other techniques are available to use trends and seasonality in a constructive way in time series predictions?

  4. Chris June 22, 2017 at 2:04 pm #

    Thanks for these posts, Dr. Brownlee! I like the picture of the beach

  5. proxy list September 27, 2017 at 7:10 pm #

    Hi there,I log on to your new stuff named “How to Difference a Time Series Dataset with Python – Machine Learning Mastery” regularly.Your humoristic style is awesome, keep up the good work! And you can look our website about proxy list.

  6. Kathy November 13, 2017 at 2:04 pm #

    Thank you for valuable insights. Could you please explain how would it be possible to take the third or second difference ?

    • Jason Brownlee November 14, 2017 at 10:07 am #

      You apply the difference operation to the already differenced series.

  7. Qian December 3, 2017 at 1:16 pm #

    for “value = int(dataset[i])-int(dataset[i-interval])”
    why it shows “TypeError: only length-1 arrays can be converted to Python scalars”
    thanks in advance!

    • Jason Brownlee December 4, 2017 at 7:45 am #

      Perhaps ensure that you have copied all of the code from the example?

Leave a Reply