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.

Kick-start your project with my new book Time Series Forecasting With Python, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

  • Updated Apr/2019: Updated the link to dataset.
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 how to get started (with sample code).

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

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).

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.

See What's Inside

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

  1. Avatar
    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. Avatar
    NL May 3, 2017 at 1:54 am #

    Have a question. What if the difference is negative?

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

      Some differences will be positive, some negative.

      • Avatar
        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.

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

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

  3. Avatar
    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. Avatar
    Chris June 22, 2017 at 2:04 pm #

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

  5. Avatar
    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. Avatar
    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 ?

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

      You apply the difference operation to the already differenced series.

  7. Avatar
    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!

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

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

  8. Avatar
    Mohammed March 24, 2018 at 12:07 pm #

    Hi Jason, thanks for posting this, but I’m curious what to do about the NAs after using the diff() function? I’m guessing that data should just be removed? Or should they just be imputed?

  9. Avatar
    HMEDA May 12, 2018 at 3:26 pm #

    (data_string, format))
    ValueError: time data ‘190Sales of shampoo over a three year period’ does not match format ‘%Y-%m’


  10. Avatar
    Adamson June 8, 2018 at 1:22 am #

    Do you perform differencing on just the output data or do you difference the features if they are time dependent as well?

  11. Avatar
    Abhay Sharma June 14, 2018 at 1:57 pm #

    How does one invert the differencing after the residual forecast has been made to get back to a forecast including the trend and seasonality that was differenced out?

  12. Avatar
    Elo November 10, 2018 at 1:12 am #

    Copy Paste ?

    Thanks for this awesome content by the way !

    • Avatar
      Jason Brownlee November 10, 2018 at 6:07 am #

      That’s a shame. I’ll ask him to take it down. Google will also penalize him ferociously.

  13. Avatar
    ENI November 27, 2018 at 12:37 am #

    Doing this, I will have no value for the first observation, I mean Yt-Yt-1 will be my first value and I will have an observation less?

  14. Avatar
    mohammad February 12, 2019 at 5:19 am #

    How to undifference?

  15. Avatar
    Tayyab May 23, 2019 at 1:26 am #

    Hi Jason! As always a great tutorial.

    I need to know, how to get the forecast values of unseen data if the data were differenced by first_order.


    I am doing univariate ARIMA forecasting for oil prices 3 times a day. The data was uneven so interpolated with forward-fill with an hourly rate. I did forecasting using first-order-differencing. To compare test_data and predictions, I reversed the predictions and test-data (integration).

    Now the question is what I do when I don’t have test data but I have forecast unseen data. How would I integrate the predictions back to normal then the different predictions?

  16. Avatar
    puneeth June 6, 2019 at 9:22 pm #

    can u please tell me hoe to extract forecasted value in graph.i got predicted value,but not able to extract forecasted value in python using arima model

    predictions_ARIMA_diff=pd.Series(results_ARIMA.fittedvalues, copy=True)


    predictions_ARIMA_log=predictions_ARIMA_log.add(predictions_ARIMA_diff_cumsum, fill_value=0)

    # Next -take the exponent of the series from above (anti-log) which will be the predicted value?—?the time series forecast model.
    ##Now plot the predicted values with the original.

    #Find the RMSE

    plt.title(‘RMSE: %.4f’% np.sqrt(sum((predictions_ARIMA-ts)**2)/len(ts)))

    #Future Prediction
    #Predict for 5 year. We have 144 data points + 60 for next 5 yrs. i.e. predict for 204 data points

    • Avatar
      Jason Brownlee June 7, 2019 at 7:58 am #

      You can plot a forecast using matplotlib, e.g. the plot() function.

  17. Avatar
    Stephanie August 21, 2019 at 11:10 pm #

    Hi Jason,

    Can you perform differencing while also adding a lag of a variable (dependent or independent) in the equation?


  18. Avatar
    John W August 26, 2019 at 9:12 pm #

    Great python tutorial on time series.

  19. Avatar
    Gizo December 26, 2019 at 7:19 pm #

    unable to run

    # create a differenced series
    def difference(dataset, interval=1):
    diff = list()
    for i in range(interval, len(dataset)):
    value = dataset[i] – dataset[i – interval]
    return Series(diff)

    error on
    value = dataset[i] – dataset[i – interval]
    TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’

  20. Avatar
    Emma February 28, 2020 at 1:20 am #

    Hi Jason, thanks for your very informative tutorials. I’m a PhD student using a time series of ocean data to create a multiple linear regression model (statsmodels GLSAR, as there is autocorrelation of residuals). I’m using the model to then predict past (rather than future) values, but these are for single data points rather than a continuous time series.

    However, the dependent variable I am using is not stationary (shows seasonality), and the independent variables show a mix of trend, seasonality and stationarity.

    I have a couple of questions:
    1) If I want to remove stationarity, I assume I use a mix of differencing and removing trends where applicable, and then create my model. How do I then apply this model to my predictions? Is it the same, or do I need to add the trends/differences back before using it to predict, somehow?

    2) I’m using an algorithm to find the combination of independent variables that give the highest R-squared value for my regression. Machine learning is growing in use in my speciality, and I would like to try it. Do you think this sounds suitable? I have 13 years of twice-daily data for training.

    I hope this is clear, happy to answer any questions.

    • Avatar
      Jason Brownlee February 28, 2020 at 6:16 am #

      Yes, differencing to remove trend, seasonal differencing to remove seasonality. Just like you propagate the differencing down the training set, you can also propagate it down the test set. Then invert the differencing on the predictions to get the original scale.

      I recommend testing a suite of methods and use controlled experiments to discover what works best.

  21. Avatar
    soukaina February 28, 2020 at 8:49 pm #

    Hi Jason, thank you for this great tutorial,
    I would like to know how to difference a time series date attribute to get a series of durations ?

    • Avatar
      Jason Brownlee February 29, 2020 at 7:12 am #

      Sorry, I don’t understand your question. Can you please elaborate?

      • Avatar
        soukaina March 1, 2020 at 5:03 am #

        I have a data in which I have two indexes, the id and date, each id has a series of dates, and I want to extract the difference between dates for each id.

        • Avatar
          Jason Brownlee March 1, 2020 at 5:25 am #

          Great. Sounds like you will need to develop some custom code.

          • Avatar
            soukaina March 1, 2020 at 7:11 am #

            what do you propose?

          • Avatar
            Jason Brownlee March 2, 2020 at 6:07 am #

            Developing custom code to meet the requirements of your project. Engineering, not machine learning.

            I don’t have the capacity to do engineering for you sorry. If it is challenging you can try posting your question to stackoverflow or hire an engineer?

  22. Avatar
    Al-Baraa October 30, 2020 at 7:29 pm #

    Hey Jason, love your tutorials! I wanted to ask you how we could plot the trend line if we difference. I’ve been looking everywhere online and I can’t find how?

    • Avatar
      Jason Brownlee October 31, 2020 at 6:46 am #


      If you plot the raw data (before differencing) you should be able to see the trend if present.

  23. Avatar
    HH November 2, 2020 at 11:02 pm #

    if i use built it differencing —- diff = series.diff()
    how could i inverse it >?
    Also is it possible to use built in differencing in multivariate data? how?

  24. Avatar
    Shaheen November 30, 2020 at 11:42 am #

    What is the discrepancy between what is referred to as ‘log difference’ and ‘first difference’ when differencing a time series? I am looking to use ACF/PACF with stationary/transformed data to estimate my ARIMA parameters but I keep running into these two ‘differences’ and I can’t tell if they’re used interchangeably or not. Also if there is a discrepancy how can we use log difference in our code using pandas?

    PS: Jason, your website has helped me during my academic career and now in my early-career as an intern who is hoping to get a full time job soon – I just wanted to thank you for all the work you’ve done.

    • Avatar
      Jason Brownlee December 1, 2020 at 6:15 am #

      I’ve not heard the terms, sorry.

      You can log the data, you can difference the data, and you can do both with different order.

  25. Avatar
    Tarun January 9, 2021 at 9:54 pm #

    Hi Jason,

    Thanks for your tutorials. I have a question. After differencing, I am getting a stationary series and that I want to confirm it by doing an ADF Test. This is my code –

    from pandas import read_csv
    from statsmodels.tsa.stattools import adfuller
    series = read_csv(‘D:/Management Books/BSE Index Daily Closing.csv’, header=0, index_col=0, squeeze=True)
    X = series.values
    diff = series.diff()
    X = diff(X)
    result = adfuller(X)
    print(‘ADF Statistic: %f’ % result[0])
    print(‘p-value: %f’ % result[1])
    print(‘Critical Values:’)
    for key, value in result[4].items():
    print(‘\t%s: %.3f’ % (key, value))

    When I run it, I get an error like

    TypeError Traceback (most recent call last)
    4 X = series.values
    5 diff = series.diff()
    —-> 6 X = diff(X)
    7 result = adfuller(X)
    8 print(‘ADF Statistic: %f’ % result[0])

    TypeError: ‘Series’ object is not callable

    Please lemme know how to rectify the error.

    • Avatar
      Jason Brownlee January 10, 2021 at 5:40 am #

      You’re welcome.

      Perhaps try extracting the numpy array from the series after differencing?

      • Avatar
        Tarun January 11, 2021 at 6:00 pm #

        Hi Jason,

        Thanks a lot for your helped. It worked. Bingo !!!!!!

  26. Avatar
    Aditya March 3, 2021 at 4:11 am #

    Hi Jason

    How do we convert the values back to original scale to be able to compare prediction with actual?

  27. Avatar
    Vanitha September 9, 2021 at 6:09 pm #

    Hi Jason,

    I like your tutorials very much and its very clear always. I am working on timeseries data(year wise).just started with univariate time series(values in million), there is trend but no seasonality and applied ARIMA model. Few questions in that.

    1. I I have applied my model without doing any feature engineering and scaling since its a univariate and i got expected and predicted values nicely. Is this correct or i need to apply diff() function to make the data stationary before apply the model?

    2. Then ARIMA(p,d,q) adjusting this parameter i got finalized value like(1,2,0) based on MAPE metric. Is this Correct?
    Here itself i can able to identify best value of p (Partial auto correlation), d(differencing value)
    and q(Auto correlation).
    3. Then what is the purpose of Dicky fuller test and Rolling mean?

    4. If the third parameter q=0 means then it can be call it as ARIMA model?.
    (if i give any value to q means it asked me to make data stationary. so i make it zero).

    5. Can i use df.diff().diff()? . if so can i call it as 2 nd order difference?. Is this meaningful?
    (then only the p-value become less than 0.5)

    kindly answer my question . Awaiting for your valuable reply.

    • Avatar
      Adrian Tam September 11, 2021 at 6:09 am #

      (1, 2) the I in ARIMA will do the differencing, hence it should make the data stationary by figuring out the right parameter (p,d,q). If you disagree with the fitted result, you can always override it.
      (3) Dicky-Fuller test is to check if the series is stationary. Rolling mean is just another name for moving average.
      (4) q is the MA parameter, d=0 means stationary
      (5) Yes, that is the case of d=2

      • Avatar
        Vanitha September 11, 2021 at 2:46 pm #

        Thank you so much for your reply.

  28. Avatar
    JK July 20, 2022 at 6:38 am #

    Hi Jason,

    Thank you for this amazing tutorial! I have one question after reading this: If the data after differencing by subtracting the previous observation from the current observation is seasonal, does that mean the original data is seasonal as well?

  29. Avatar
    SNA November 17, 2023 at 2:57 am #

    i have a question. if the data that we have is in each year (from 2009 to 2020). it is obvious that we have no seasonality, but according to ADF test, it is a non stationary data. Would differencing will make the data stationary or it won’t make any difference?

    • Avatar
      James Carmichael November 17, 2023 at 11:03 am #

      Hi SNA…We recommend that you try differencing and also not differencing and compare the results.

Leave a Reply