{"id":3739,"date":"2017-02-22T05:00:42","date_gmt":"2017-02-21T18:00:42","guid":{"rendered":"https:\/\/35.82.237.216\/?p=3739"},"modified":"2020-08-14T10:48:14","modified_gmt":"2020-08-14T00:48:14","slug":"difference-time-series-dataset-python","status":"publish","type":"post","link":"https:\/\/machinelearningmastery.com\/difference-time-series-dataset-python\/","title":{"rendered":"How to Difference a Time Series Dataset with Python"},"content":{"rendered":"<p>Differencing is a popular and widely used data transform for time series.<\/p>\n<p>In this tutorial, you will discover how to apply the difference operation to your time series data with Python.<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>About the differencing operation, including the configuration of the lag difference and the difference order.<\/li>\n<li>How to develop a manual implementation of the differencing operation.<\/li>\n<li>How to use the built-in Pandas differencing function.<\/li>\n<\/ul>\n<p><strong>Kick-start your project<\/strong> with my new book <a href=\"https:\/\/machinelearningmastery.com\/introduction-to-time-series-forecasting-with-python\/\">Time Series Forecasting With Python<\/a>, including <em>step-by-step tutorials<\/em> and the <em>Python source code<\/em> files for all examples.<\/p>\n<p>Let&#8217;s get started.<\/p>\n<ul>\n<li><strong>Updated Apr\/2019<\/strong>: Updated the link to dataset.<\/li>\n<\/ul>\n<div id=\"attachment_3743\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3743\" class=\"size-full wp-image-3743\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg\" alt=\"How to Difference a Time Series Dataset with Python\" width=\"640\" height=\"478\" srcset=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg 640w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python-300x224.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><p id=\"caption-attachment-3743\" class=\"wp-caption-text\">How to Difference a Time Series Dataset with Python<br \/>Photo by <a href=\"https:\/\/www.flickr.com\/photos\/tempoworld\/7326465464\/\">Marcus<\/a>, some rights reserved.<\/p><\/div>\n<h2>Why Difference Time Series Data?<\/h2>\n<p>Differencing is a method of transforming a time series dataset.<\/p>\n<p>It can be used to remove the series dependence on time, so-called temporal dependence. This includes structures like trends and seasonality.<\/p>\n<blockquote><p>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.<\/p><\/blockquote>\n<p>&#8212; Page 215, <a href=\"https:\/\/amzn.to\/2DOHnwQ\">Forecasting: principles and practice<\/a><\/p>\n<p>Differencing is performed by subtracting the previous observation from the current observation.<\/p>\n<pre class=\"lang:default highlight:0 decode:true \">difference(t) = observation(t) - observation(t-1)<\/pre>\n<p>In this way, a series of differences can be calculated.<\/p>\n<h3>Lag Difference<\/h3>\n<p>Taking the difference between consecutive observations is called a lag-1 difference.<\/p>\n<p>The lag difference can be adjusted to suit the specific temporal structure.<\/p>\n<p>For time series with a seasonal component, the lag may be expected to be the period (width) of the seasonality.<\/p>\n<h3>Difference Order<\/h3>\n<p>Temporal structure may still exist after performing a differencing operation, such as in the case of a nonlinear trend.<\/p>\n<p>As such, the process of differencing can be repeated more than once until all temporal dependence has been removed.<\/p>\n<p>The number of times that differencing is performed is called the difference order.<\/p>\n<p><div class=\"woo-sc-hr\"><\/div><\/p>\r\n<center>\r\n<h3>Stop learning Time Series Forecasting the <em>slow way<\/em>!<\/h3>\r\n<p>Take my free 7-day email course and discover how to get started (with sample code).<\/p>\r\n<p>Click to sign-up and also get a free PDF Ebook version of the course.<\/p>\r\n<p><script src=\"https:\/\/embed.lpcontent.net\/leadboxes\/current\/embed.js\" async defer><\/script> <button data-leadbox-popup=\"7U3pw6gCGRCSM5PTLfhiKQ\" data-leadbox-domain=\"machinelearningmastery.lpages.co\" style=\"background: rgb(255, 215, 18);border-color: rgb(255, 215, 18);border-radius: 20px;color: #FFFFFF;display: inline-block;vertical-align: middle;padding: 16px 32px;min-width: 192px;border: 1px solid rgb(255, 215, 18);font-size: 1rem;font-family: Helvetica, Arial, sans-serif;text-align: center;outline: 0;line-height: 1;cursor: pointer;-webkit-transition: background 0.3s, color 0.3s, border 0.3s;transition: background 0.3s, color 0.3s, border 0.3s;  box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.6);\">Start Your FREE Mini-Course Now!<\/button><\/center>\r\n<p><div class=\"woo-sc-hr\"><\/div><\/p>\n<h2>Shampoo Sales Dataset<\/h2>\n<p>This dataset describes the monthly number of sales of shampoo over a 3 year period.<\/p>\n<p>The units are a sales count and there are 36 observations. The original dataset is credited to Makridakis, Wheelwright, and Hyndman (1998).<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/shampoo.csv\">Download the dataset<\/a>.<\/li>\n<\/ul>\n<p>The example below loads and creates a plot of the loaded dataset.<\/p>\n<pre class=\"lang:default decode:true \">from pandas import read_csv\r\nfrom pandas import datetime\r\nfrom matplotlib import pyplot\r\n\r\ndef parser(x):\r\n\treturn datetime.strptime('190'+x, '%Y-%m')\r\n\r\nseries = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)\r\nseries.plot()\r\npyplot.show()<\/pre>\n<p>Running the example creates the plot that shows a clear linear trend in the data.<\/p>\n<div id=\"attachment_3546\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3546\" class=\"size-full wp-image-3546\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2016\/12\/Shampoo-Sales-Dataset-Plot-1.png\" alt=\"Shampoo Sales Dataset Plot\" width=\"800\" height=\"600\" srcset=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2016\/12\/Shampoo-Sales-Dataset-Plot-1.png 800w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2016\/12\/Shampoo-Sales-Dataset-Plot-1-300x225.png 300w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2016\/12\/Shampoo-Sales-Dataset-Plot-1-768x576.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><p id=\"caption-attachment-3546\" class=\"wp-caption-text\">Shampoo Sales Dataset Plot<\/p><\/div>\n<h2>Manual Differencing<\/h2>\n<p>We can difference the dataset manually.<\/p>\n<p>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.<\/p>\n<p>The function below named <em>difference()<\/em> implements this procedure.<\/p>\n<pre class=\"lang:default decode:true \"># create a differenced series\r\ndef difference(dataset, interval=1):\r\n\tdiff = list()\r\n\tfor i in range(interval, len(dataset)):\r\n\t\tvalue = dataset[i] - dataset[i - interval]\r\n\t\tdiff.append(value)\r\n\treturn Series(diff)<\/pre>\n<p>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.<\/p>\n<p>One further improvement would be to also be able to specify the order or number of times to perform the differencing operation.<\/p>\n<p>The example below applies the manual <em>difference()<\/em> function to the Shampoo Sales dataset.<\/p>\n<pre class=\"lang:default decode:true\">from pandas import read_csv\r\nfrom pandas import datetime\r\nfrom pandas import Series\r\nfrom matplotlib import pyplot\r\n\r\ndef parser(x):\r\n\treturn datetime.strptime('190'+x, '%Y-%m')\r\n\r\n# create a differenced series\r\ndef difference(dataset, interval=1):\r\n\tdiff = list()\r\n\tfor i in range(interval, len(dataset)):\r\n\t\tvalue = dataset[i] - dataset[i - interval]\r\n\t\tdiff.append(value)\r\n\treturn Series(diff)\r\n\r\nseries = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)\r\nX = series.values\r\ndiff = difference(X)\r\npyplot.plot(diff)\r\npyplot.show()<\/pre>\n<p>Running the example creates the differenced dataset and plots the result.<\/p>\n<div id=\"attachment_3740\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3740\" class=\"size-full wp-image-3740\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Manually-Differenced-Shampoo-Sales-Dataset.png\" alt=\"Manually Differenced Shampoo Sales Dataset\" width=\"800\" height=\"600\" srcset=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Manually-Differenced-Shampoo-Sales-Dataset.png 800w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Manually-Differenced-Shampoo-Sales-Dataset-300x225.png 300w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Manually-Differenced-Shampoo-Sales-Dataset-768x576.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><p id=\"caption-attachment-3740\" class=\"wp-caption-text\">Manually Differenced Shampoo Sales Dataset<\/p><\/div>\n<h2>Automatic Differencing<\/h2>\n<p>The Pandas library provides a function to automatically calculate the difference of a dataset.<\/p>\n<p>This <em>diff()<\/em> function is provided on both the <a href=\"http:\/\/pandas.pydata.org\/pandas-docs\/stable\/generated\/pandas.Series.diff.html\">Series<\/a> and <a href=\"http:\/\/pandas.pydata.org\/pandas-docs\/stable\/generated\/pandas.DataFrame.diff.html\">DataFrame<\/a> objects.<\/p>\n<p>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 <em>periods<\/em>.<\/p>\n<p>The example below demonstrates how to use the built-in difference function on the Pandas Series object.<\/p>\n<pre class=\"lang:default decode:true \">from pandas import read_csv\r\nfrom pandas import datetime\r\nfrom matplotlib import pyplot\r\n\r\ndef parser(x):\r\n\treturn datetime.strptime('190'+x, '%Y-%m')\r\n\r\nseries = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)\r\ndiff = series.diff()\r\npyplot.plot(diff)\r\npyplot.show()<\/pre>\n<p>As in the previous section, running the example plots the differenced dataset.<\/p>\n<p>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.<\/p>\n<div id=\"attachment_3741\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3741\" class=\"size-full wp-image-3741\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Automatic-Differenced-Shampoo-Sales-Dataset.png\" alt=\"Automatic Differenced Shampoo Sales Dataset\" width=\"800\" height=\"600\" srcset=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Automatic-Differenced-Shampoo-Sales-Dataset.png 800w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Automatic-Differenced-Shampoo-Sales-Dataset-300x225.png 300w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/01\/Automatic-Differenced-Shampoo-Sales-Dataset-768x576.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><p id=\"caption-attachment-3741\" class=\"wp-caption-text\">Automatic Differenced Shampoo Sales Dataset<\/p><\/div>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered how to apply the difference operation to time series data with Python.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>About the difference operation, including the configuration of lag and order.<\/li>\n<li>How to implement the difference transform manually.<\/li>\n<li>How to use the built-in Pandas implementation of the difference transform.<\/li>\n<\/ul>\n<p>Do you have any questions about differencing, or about this post?<br \/>\nAsk your questions in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3743,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"no","_lmt_disable":"no","_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[301],"tags":[],"class_list":["post-3739","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-time-series"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Difference a Time Series Dataset with Python - MachineLearningMastery.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Difference a Time Series Dataset with Python - MachineLearningMastery.com\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/\" \/>\n<meta property=\"og:site_name\" content=\"MachineLearningMastery.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/MachineLearningMastery\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/MachineLearningMastery\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-21T18:00:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-14T00:48:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"478\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jason Brownlee\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jason Brownlee\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/\"},\"author\":{\"name\":\"Jason Brownlee\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#\\\/schema\\\/person\\\/8a7b819d3cca7cfe78f1e65296f0c8fe\"},\"headline\":\"How to Difference a Time Series Dataset with Python\",\"datePublished\":\"2017-02-21T18:00:42+00:00\",\"dateModified\":\"2020-08-14T00:48:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/\"},\"wordCount\":718,\"commentCount\":70,\"publisher\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningmastery.com\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg\",\"articleSection\":[\"Time Series\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/\",\"url\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/\",\"name\":\"How to Difference a Time Series Dataset with Python - MachineLearningMastery.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningmastery.com\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg\",\"datePublished\":\"2017-02-21T18:00:42+00:00\",\"dateModified\":\"2020-08-14T00:48:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/machinelearningmastery.com\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg\",\"contentUrl\":\"https:\\\/\\\/machinelearningmastery.com\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg\",\"width\":640,\"height\":478,\"caption\":\"How to Difference a Time Series Dataset with Python Photo by Marcus, some rights reserved.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/difference-time-series-dataset-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/machinelearningmastery.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Difference a Time Series Dataset with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#website\",\"url\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/\",\"name\":\"MachineLearningMastery.com\",\"description\":\"Making developers awesome at machine learning\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#organization\",\"name\":\"Machine Learning Mastery\",\"url\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningmastery.com\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-icon.png\",\"contentUrl\":\"https:\\\/\\\/machinelearningmastery.com\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-icon.png\",\"width\":512,\"height\":512,\"caption\":\"Machine Learning Mastery\"},\"image\":{\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/MachineLearningMastery\\\/\",\"https:\\\/\\\/x.com\\\/TeachTheMachine\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/machine-learning-mastery\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.machinelearningmastery.com\\\/#\\\/schema\\\/person\\\/8a7b819d3cca7cfe78f1e65296f0c8fe\",\"name\":\"Jason Brownlee\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5f1c8e7a708d04b1bd173e9120107d3fd43d8fad5be7c94796b877515b6d0357?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5f1c8e7a708d04b1bd173e9120107d3fd43d8fad5be7c94796b877515b6d0357?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5f1c8e7a708d04b1bd173e9120107d3fd43d8fad5be7c94796b877515b6d0357?s=96&d=mm&r=g\",\"caption\":\"Jason Brownlee\"},\"description\":\"Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results with modern machine learning methods via hands-on tutorials.\",\"sameAs\":[\"https:\\\/\\\/machinelearningmastery.com\",\"https:\\\/\\\/www.facebook.com\\\/MachineLearningMastery\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/machine-learning-mastery\",\"https:\\\/\\\/x.com\\\/teachthemachine\"],\"url\":\"https:\\\/\\\/machinelearningmastery.com\\\/author\\\/jasonb\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Difference a Time Series Dataset with Python - MachineLearningMastery.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Difference a Time Series Dataset with Python - MachineLearningMastery.com","og_description":"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 [&hellip;]","og_url":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/","og_site_name":"MachineLearningMastery.com","article_publisher":"https:\/\/www.facebook.com\/MachineLearningMastery\/","article_author":"https:\/\/www.facebook.com\/MachineLearningMastery\/","article_published_time":"2017-02-21T18:00:42+00:00","article_modified_time":"2020-08-14T00:48:14+00:00","og_image":[{"width":640,"height":478,"url":"https:\/\/www.machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg","type":"image\/jpeg"}],"author":"Jason Brownlee","twitter_misc":{"Written by":"Jason Brownlee","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#article","isPartOf":{"@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/"},"author":{"name":"Jason Brownlee","@id":"https:\/\/www.machinelearningmastery.com\/#\/schema\/person\/8a7b819d3cca7cfe78f1e65296f0c8fe"},"headline":"How to Difference a Time Series Dataset with Python","datePublished":"2017-02-21T18:00:42+00:00","dateModified":"2020-08-14T00:48:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/"},"wordCount":718,"commentCount":70,"publisher":{"@id":"https:\/\/www.machinelearningmastery.com\/#organization"},"image":{"@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg","articleSection":["Time Series"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/","url":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/","name":"How to Difference a Time Series Dataset with Python - MachineLearningMastery.com","isPartOf":{"@id":"https:\/\/www.machinelearningmastery.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#primaryimage"},"image":{"@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg","datePublished":"2017-02-21T18:00:42+00:00","dateModified":"2020-08-14T00:48:14+00:00","breadcrumb":{"@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#primaryimage","url":"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg","contentUrl":"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg","width":640,"height":478,"caption":"How to Difference a Time Series Dataset with Python Photo by Marcus, some rights reserved."},{"@type":"BreadcrumbList","@id":"https:\/\/www.machinelearningmastery.com\/difference-time-series-dataset-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/machinelearningmastery.com\/"},{"@type":"ListItem","position":2,"name":"How to Difference a Time Series Dataset with Python"}]},{"@type":"WebSite","@id":"https:\/\/www.machinelearningmastery.com\/#website","url":"https:\/\/www.machinelearningmastery.com\/","name":"MachineLearningMastery.com","description":"Making developers awesome at machine learning","publisher":{"@id":"https:\/\/www.machinelearningmastery.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.machinelearningmastery.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.machinelearningmastery.com\/#organization","name":"Machine Learning Mastery","url":"https:\/\/www.machinelearningmastery.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.machinelearningmastery.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2016\/09\/cropped-icon.png","contentUrl":"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2016\/09\/cropped-icon.png","width":512,"height":512,"caption":"Machine Learning Mastery"},"image":{"@id":"https:\/\/www.machinelearningmastery.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/MachineLearningMastery\/","https:\/\/x.com\/TeachTheMachine","https:\/\/www.linkedin.com\/company\/machine-learning-mastery"]},{"@type":"Person","@id":"https:\/\/www.machinelearningmastery.com\/#\/schema\/person\/8a7b819d3cca7cfe78f1e65296f0c8fe","name":"Jason Brownlee","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5f1c8e7a708d04b1bd173e9120107d3fd43d8fad5be7c94796b877515b6d0357?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5f1c8e7a708d04b1bd173e9120107d3fd43d8fad5be7c94796b877515b6d0357?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f1c8e7a708d04b1bd173e9120107d3fd43d8fad5be7c94796b877515b6d0357?s=96&d=mm&r=g","caption":"Jason Brownlee"},"description":"Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results with modern machine learning methods via hands-on tutorials.","sameAs":["https:\/\/machinelearningmastery.com","https:\/\/www.facebook.com\/MachineLearningMastery\/","https:\/\/www.linkedin.com\/company\/machine-learning-mastery","https:\/\/x.com\/teachthemachine"],"url":"https:\/\/machinelearningmastery.com\/author\/jasonb\/"}]}},"modified_by":"Jason Brownlee","taxonomy_info":{"category":[{"value":301,"label":"Time Series"}]},"featured_image_src_large":["https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2017\/02\/How-to-Difference-a-Time-Series-Dataset-with-Python.jpg",640,478,false],"author_info":{"display_name":"Jason Brownlee","author_link":"https:\/\/machinelearningmastery.com\/author\/jasonb\/"},"comment_info":71,"category_info":[{"term_id":301,"name":"Time Series","slug":"time-series","term_group":0,"term_taxonomy_id":303,"taxonomy":"category","description":"","parent":0,"count":54,"filter":"raw","cat_ID":301,"category_count":54,"category_description":"","cat_name":"Time Series","category_nicename":"time-series","category_parent":0}],"tag_info":false,"_links":{"self":[{"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/posts\/3739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/comments?post=3739"}],"version-history":[{"count":0,"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/posts\/3739\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/media\/3743"}],"wp:attachment":[{"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/media?parent=3739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/categories?post=3739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningmastery.com\/wp-json\/wp\/v2\/tags?post=3739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}