Time series delta (difference) transformer.

Creates difference features from time series data.

Classes

DifferenceTransformer

class preprocessing.timeseries.delta.DifferenceTransformer(Transformer)

Compute the difference between periods in time-ordered data.

Calculates the change in feature values between the current instance and a lagged instance. This is a standard technique for making a non-stationary time series stationary.
Constructor
__init__(
    self,
    lag: int = ...,
    columns: list[int] | None = None,
    fill_with_missing: bool = True,
    invert_selection: bool = False,
)

Theory

The differenced value \Delta x_t is calculated as:

\Delta x_t = x_t - x_{t-k}

where k is the lag period.

Parameters

lag
int = -1

The number of periods to shift for differencing.

  • Negative values (e.g., -1): Subtract the previous value

(current - past).

  • Positive values (e.g., 1): Subtract the next value
(current - future).
columns
list of int
Indices of numeric columns to difference. If None, all numeric columns are processed.
fill_with_missing
bool = True
  • If True: Keeps the original number of rows and fills boundary

indices with np.nan.

  • If False: Removes the rows that would contain np.nan results.
invert_selection
bool = False
If True, applies the difference to all columns except those specified in columns.

Attributes

feature_names_out_
list of str
The generated names for the difference features (e.g., "x d-1").

Calculate day-over-day changes:

python
>>> from tuiml.preprocessing.timeseries import DifferenceTransformer
>>> import numpy as np
>>> X = np.array([[10], [12], [11], [15]])
>>> differencer = DifferenceTransformer(lag=-1)
>>> X_diff = differencer.fit_transform(X)
>>> print(X_diff.flatten())
[nan  2. -1.  4.]

Methods

get_parameter_schema (cls)
fit (self, X: np.ndarray, y: np.ndarray | None=None, feature_names: list[str] | None=None) -> 'DifferenceTransformer'

Fit the transformer.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Input data. Only its shape is recorded; no statistics are learned.
y
np.ndarray
Ignored, present for API consistency.
feature_names
list of str
Names of the input columns, used to label the generated columns.
Returns
self
object
The fitted transformer.
transform (self, X: np.ndarray) -> np.ndarray

Transform the data into differences.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Input data, assumed to be ordered in time.
Returns
X_out
np.ndarray
Transformed data holding the difference values.
get_feature_names_out (self, input_features: list[str] | None=None) -> list[str]

Get output feature names.

__repr__ (self) -> str