Skip to content

Seasonal decompose features

polars_ts.decomposition.seasonal_decompose_features

seasonal_decompose_features(df, ts_freq, seasonal_freqs=None, mode='simple', id_col='unique_id', time_col='ds', target_col='y')

Perform seasonal decomposition on a time series and compute additional features.

  • Trend strength
  • Seasonal strength
  • Residual variance

Parameters:

Name Type Description Default
df DataFrame

A Polars DataFrame containing the time series data with the following columns:

  • id_col: Identifier for each time series (e.g., product, region, etc.)
  • time_col: Timestamp column indicating the time of each observation.
  • target_col: The column containing the values to be decomposed.
required
ts_freq str

The frequency of the time series. For example, 24 could represent daily seasonality in an hourly time series.

required
seasonal_freqs Optional[List[int]]

A list of seasonal frequencies to use for the MSTL mode. This is required only if mode='mstl'. Defaults to None.

None
mode Literal['simple', 'mstl']

The decomposition mode, one of {'simple', 'mstl'}. Defaults to 'simple'.

'simple'
id_col str

The name of the column that identifies each individual time series within the DataFrame. Defaults to unique_id.

'unique_id'
time_col str

The name of the column that contains the time or datetime information for each observation. Defaults to ds.

'ds'
target_col str

The name of the column containing the time series data to be decomposed. Defaults to y.

'y'

Returns:

Type Description
DataFrame

A DataFrame containing the following features for each unique id_col:

  • trend_strength: A measure of how strong the trend component is in the time series (value between 0 and 1).
  • seasonal_strength: A measure of how strong the seasonal component is in the time series (value between 0 and 1).
  • resid_var: The ratio of the standard deviation of the residuals to the mean of the target variable (a measure of residual variance).