Long-Term Decomposition

Long-Term Decomposition#

Goal

Decompose multi-year energy consumption data into trend, seasonal, and residual components to understand underlying patterns over a 10-year period.

Data Basis

Monthly energy consumption data (decomposition_long.csv) bundled with the pyedautils package. 119 monthly values over ~10 years.

Data Preparation#

import pandas as pd
from importlib import resources

data_path = resources.files("pyedautils") / "data" / "decomposition_long.csv"
df = pd.read_csv(data_path, sep=";")
df.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

Visualization#

from pyedautils.plots import plot_decomposition

fig = plot_decomposition(
    data=df,
    period=12,
    s_window=7,
    title="Time Series Decomposition over 10 Years",
    ylab="Energy Consumption [kWh/month]",
)
fig.show()
Customization

The plot_decomposition function accepts additional parameters to fine-tune the decomposition:

  • period — The period of the seasonal component (e.g., 365 for daily data with yearly seasonality). If not set, the function attempts to detect it automatically.

  • s_window — The span of the LOESS window for seasonal extraction. A larger value produces a smoother seasonal component.

  • plot_decomposition puts the data on a regular grid and fills gaps of up to max_gap steps (default 6); if longer gaps remain, it decomposes the longest gap-free stretch and warns.