Seasonal decomposition
polars_ts.decomposition.seasonal_decomposition
seasonal_decomposition(df, freq, method='additive', id_col='unique_id', target_col='y', time_col='ds', anomaly_threshold=None)
Perform seasonal decomposition of time series data using either an additive or multiplicative method.
- Additive:
Y(t) = T(t) + S(t) + E(t) - Multiplicative:
Y(t) = T(t) * S(t) * E(t)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Polars DataFrame containing the time series data. |
required |
freq
|
int
|
The seasonal period (e.g., 12 for monthly data with yearly seasonality). |
required |
method
|
Literal['additive', 'multiplicative']
|
The decomposition method ( |
'additive'
|
id_col
|
str
|
The column to group by (e.g., for multiple time series). Defaults to |
'unique_id'
|
target_col
|
str
|
The column containing the time series values to decompose. Defaults to |
'y'
|
time_col
|
str
|
The column containing the time values. Defaults to |
'ds'
|
anomaly_threshold
|
float | None
|
If provided, adds an |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame with the decomposed components: trend, seasonal component, and residuals. |
DataFrame
|
When |
Raises:
| Type | Description |
|---|---|
ValueError
|
If invalid |
KeyError
|
If specified columns do not exist in the DataFrame. |
ValueError
|
If the DataFrame is empty or doesn't have enough data to decompose. |