Time Series Decomposition#
STL (Seasonal and Trend decomposition using Loess) splits a time series into three components: trend, seasonal, and remainder.
This example shows two use cases:
Long term — monthly energy data over ~10 years (period=12)
Short term — 15-minute power data over 5 days (period=96)
All plots are interactive — hover to see exact values.
import plotly.io as pio
pio.renderers.default = "notebook_connected"
Long Term Decomposition#
Monthly heating energy consumption of a flat over 10 years. An energy optimization in mid-2017 is clearly visible in the trend and the magnitude of the seasonal pattern.
import pandas as pd
from importlib import resources
data_path = resources.files("pyedautils") / "data" / "decomposition_long.csv"
df_long = pd.read_csv(data_path, sep=";")
df_long.head()
| timestamp | value | |
|---|---|---|
| 0 | 2010-02-01 | 600.0 |
| 1 | 2010-03-01 | 550.0 |
| 2 | 2010-04-01 | 528.0 |
| 3 | 2010-05-01 | 259.0 |
| 4 | 2010-06-01 | 27.0 |
from pyedautils.plots import plot_decomposition
fig = plot_decomposition(
data=df_long,
period=12,
s_window=7,
title="Time Series Decomposition over 10 Years",
ylab="Energy Consumption [kWh/month]",
)
fig.show()
Short Term Decomposition#
15-minute electricity meter data over 5 days. The daily cycle (period=96, i.e. 96 intervals of 15 min per day) is clearly decomposed into a weekday pattern (trend) and the repeating daily profile (seasonal).
data_path = resources.files("pyedautils") / "data" / "decomposition_short.csv"
df_short = pd.read_csv(data_path, sep=";")
df_short.head()
| timestamp | value | |
|---|---|---|
| 0 | 2015-01-26 00:00:00 | 86.0 |
| 1 | 2015-01-26 00:15:00 | 100.0 |
| 2 | 2015-01-26 00:30:00 | 60.0 |
| 3 | 2015-01-26 00:45:00 | 82.0 |
| 4 | 2015-01-26 01:00:00 | 90.0 |
fig = plot_decomposition(
data=df_short,
period=96,
s_window=193,
title="Time Series Decomposition over 5 Days",
ylab="Power [kW]",
)
fig.show()