stl_diagnostics

stl_diagnostics(
    data,
    date_column,
    value_column,
    frequency='auto',
    trend='auto',
    robust=True,
)

Generate STL decomposition diagnostics (observed, season, trend, remainder, seasadj).

Parameters

Name Type Description Default
data pd.DataFrame or pd.core.groupby.generic.DataFrameGroupBy Time series data, optionally grouped. required
date_column str Name of the datetime column. required
value_column str Numeric measure to decompose. required
frequency (str, int, float, pd.Timedelta, pd.DateOffset) Seasonal period specification. "auto" (default) infers a period via :func:pytimetk.get_seasonal_frequency. Strings such as "7D" or "30 days" are supported. 'auto'
trend (str, int, float, pd.Timedelta, pd.DateOffset) Trend window specification. "auto" (default) infers a window via :func:pytimetk.get_trend_frequency. 'auto'
robust bool Apply a robust STL fit (down-weights outliers). Defaults to True. True

Returns

Name Type Description
pd.DataFrame Decomposition with columns: - grouping columns (if present) - date, observed, season, trend, remainder, seasadj

Examples

import numpy as np
import pandas as pd
import pytimetk as tk

rng = pd.date_range("2020-01-01", periods=180, freq="D")
values = np.sin(np.linspace(0, 8 * np.pi, len(rng))) + np.random.default_rng(123).normal(scale=0.1, size=len(rng))
df = pd.DataFrame({"date": rng, "value": values})

decomposition = tk.stl_diagnostics(
    data=df,
    date_column="date",
    value_column="value",
    frequency="7D",
    trend="30 days",
)
decomposition.head()
date observed season trend remainder seasadj
0 2020-01-01 -0.098912 -0.306686 0.609350 -0.401577 0.207774
1 2020-01-02 0.103167 -0.223424 0.621052 -0.294462 0.326591
2 2020-01-03 0.405929 -0.003336 0.630854 -0.221589 0.409266
3 2020-01-04 0.428271 0.002081 0.638685 -0.212495 0.426190
4 2020-01-05 0.624586 0.065817 0.644524 -0.085755 0.558769