Back to models
Transformer

Differencer

Apply iterative differences to a timeseries.

The transformation works for univariate and multivariate timeseries. However, the multivariate case applies the same differencing to every series.

Difference transformations are applied at the specified lags in the order provided.

For example, given a timeseries with monthly periodicity, using lags=[1, 12] corresponds to applying a standard first difference to handle trend, and followed by a seasonal difference (at lag 12) to attempt to account for seasonal dependence.

To provide a higher-order difference at the same lag list the lag multiple times. For example, lags=[1, 1] takes iterative first differences like may be needed for a series that is integrated of order 2.

Quickstart

python
from sktime.transformations.difference import Differencer

estimator = Differencer(lags=1, na_handling='fill_zero', memory='all')

Tags

Capabilities

  • Multivariate
  • Inverse transform: Supported
  • Unequal-length series
  • Missing values: Not supported
  • Removes missing values: Not supported
  • Equalizes series length: Not supported

Properties

Input typescitype:transform-input
Series
Output typescitype:transform-output
Series
Label typescitype:transform-labels
None
Fit is emptyfit_is_empty
No
Keeps the time indextransform-returns-same-time-index
No
Requires Xrequires_X
Yes
Requires yrequires_y
No
X and y need the same indexX-y-must-have-same-index
No

Parameters(3)

lagsint or array-like, default = 1

The lags used to difference the data. If a single int value is

na_handlingstr, optional, default = “fill_zero”

How to handle the NaNs that appear at the start of the series from differencing Example: there are only 3 differences in a series of length 4,

differencing [a, b, c, d] gives [?, b-a, c-b, d-c] so we need to determine what happens with the “?” (= unknown value)

“drop_na” - unknown value(s) are dropped, the series is shortened “keep_na” - unknown value(s) is/are replaced by NaN “fill_zero” - unknown value(s) is/are replaced by zero

memorystr, optional, default = “all”

how much of previously seen X to remember, for exact reconstruction of inverse “all”: estimator remembers all X, inverse is correct for all indices seen “latest”: estimator only remembers latest X necessary for future reconstruction

inverses at any time stamps after fit are correct, but not past time stamps

“none”: estimator does not remember any X, inverse is direct cumsum

Examples

>>> from sktime.transformations.difference import Differencer
>>> from sktime.datasets import load_airline
>>> y = load_airline ()
>>> transformer = Differencer (lags = [1, 12 ])
>>> y_transform = transformer. fit_transform (y)