Skip to content

Differencing

polars_ts.transforms.differencing

Differencing and undifferencing for time series target columns.

difference(df, order=1, period=1, target_col='y', id_col='unique_id', time_col='ds')

Apply differencing to target column, replacing it with differenced values.

Parameters

df Input DataFrame. order Number of times to difference (1 = first-order, 2 = second-order). period Seasonal period (1 = regular differencing, m = seasonal). target_col Column to difference. id_col Column identifying each time series. time_col Column with timestamps for ordering.

Returns

pl.DataFrame DataFrame with target_col replaced by differenced values. Stores initial values in {target_col}_diff_initial (list column) for inversion. Null rows produced by differencing are dropped.

undifference(df, order=1, period=1, target_col='y', id_col='unique_id', time_col='ds')

Invert differencing, restoring target column to original scale.

Parameters

df DataFrame with differenced target column and {target_col}_diff_initial metadata. order Number of differencing passes to invert. period Seasonal period used in differencing. target_col Column to restore. id_col Column identifying each time series. time_col Column with timestamps for ordering.

Returns

pl.DataFrame DataFrame with target_col restored and metadata column dropped.

_cumsum_with_prefix(prefix, diffed, period)

Reconstruct original values from differenced values and initial prefix.