Concept | Time series resampling#

Watch the video

In this lesson, we’ll discuss resampling and interpolation, what motivates these steps, and how we can apply them in Dataiku using the Resampling recipe of the Time Series Preparation plugin.

Equispaced timestamps#

Let’s return to our conceptual example of revenue from the Haiku T-Shirt shop.

At the beginning of this series, our data is equispaced. We have exactly one value for every consecutive day. Then, at some point, our timestamps become irregularly spaced. Some days are missing from the series. If we want to use this data for further analysis or forecasting, these irregularly-spaced timestamps can cause a problem.

../../_images/irregular-timestamps.png

Resampling the data gives us a way to create equispaced timestamps.

If we choose to keep our data at a daily interval, we can fill in the missing dates with new rows in the series.

../../_images/equispaced-timestamps.png

Interpolation#

Now, our timestamps are equispaced, but what values do we fill in for the new rows?

This is the interpolation step. We need to infer a value for the new rows based on our understanding or assumptions about the time series.

Connecting the points with a straight line is one option, but there are many possible options.

../../_images/linear-interpolation.png

For example, we could carry forward the previous value.

../../_images/previous-interpolation.png

Or, populate backwards from the next value.

../../_images/next-interpolation.png

Maybe instead of a linear relationship, the fit is closer to a quadratic?

../../_images/quadratic-interpolation.png

These are just a few of the possible options. The answer will depend on your own understanding and assumptions about the data.

../../_images/interpolation-methods.png

Resampling in Dataiku#

Let’s see how this process actually works in Dataiku using the same data from the previous lesson.

From the wide format, we can easily see that the timestamps are not equispaced. Many dates are missing.

../../_images/orders-wide-missing.png

We can use the Resampling recipe in the Time Series Preparation plugin to take care of this.

order_date is the timestamp column.

In this case, we are resampling at a daily level, but we could also choose a shorter or longer interval depending on the data at hand and our objectives.

For now, let’s not interpolate or extrapolate any values.

Here we are using data in a wide format, but if we had long format data, we could just as easily check this box and provide the name of the identifier column.

../../_images/resampling-params-1.png

After running the resampling recipe, we have one row for every date from the beginning to the end of the range. But all of the values for the new rows are missing because we did not yet choose any interpolation method.

../../_images/orders-wide-resampled-1.png

Constant value interpolation#

Let’s first demonstrate interpolating a constant value. If these are distinct sales, and we are not missing any data, interpolating a constant value of 0 may be a good strategy.

../../_images/resampling-params-2.png

In the output, we can see that values of 0 have been added in between the original data points.

../../_images/orders-wide-resampled-2.png

For example, the first sale of male, black T-Shirts was for 19 dollars. The next purchase was about one month later for 57 dollars. All of the dates in between those dates had a 0 filled in.

Extrapolation#

Because we did not perform any extrapolation, Dataiku did not insert any values before an individual series begins.

That’s why there are still missing values before the first recorded value in the later time series.

Let’s fill in the missing zeroes so we have a common start and end date for all series in the dataset by using the same interpolation method for extrapolation.

../../_images/resampling-params-3.png

Looking at the output, we could take a dataset like this much further, for example, by calculating total sales per month or a rolling 7-day average.

../../_images/orders-wide-resampled-3.png

Edit series#

Before moving on to an analysis step, we may want to edit the series. For the first month, sales are very sparse. Perhaps we don’t want to include this data. The plugin makes it easy to clip the series from the beginning or the end in the same time unit as the resampling parameter.

../../_images/resampling-params-4.png

Now that same number of timestamps will be removed from the beginning of the series.

../../_images/orders-wide-resampled-4.png

What’s next?#

And that’s the basics of equispacing and interpolating time series data with the Resampling recipe in the Time Series Preparation plugin!

Up next we’ll see how to identify periods when data values are within a given range under certain conditions using the Interval Extraction recipe.