# Time Series Decomposition: Classical Method in Python

Last Update: June 20, 2022

Time Series Decomposition: Classical Method in Python can be done using `statsmodels` package `seasonal_decompose` function found within its `statsmodels.tsa.api` module for estimating time series trend-cycle, seasonal and remainder components. Main parameters within `seasonal_decompose` function are `x` with time series data, `model` with seasonal component type and `two_sided` with moving average method used in filtering.

As example, we can do training range univariate time series classical additive seasonal decomposition by moving averages using data included within `datasets` R package `AirPassengers` object [1].

First, we import packages `pandas` for data frames, `statsmodels` for data downloading and time series decomposition and `matplotlib` for training range and time series decomposition charts [2].

``````In [1]:
import pandas as pd
import statsmodels.api as sm
import statsmodels.tsa.api as ts
import matplotlib.pyplot as plt
``````

Second, we create `mdata` model data object using `get_rdataset` function, convert `mdata` object into a data frame using `DataFrame` function and display first five months of data using `print` function and `head` data frame method to view time series structure.

``````In [2]:
mdata = sm.datasets.get_rdataset(dataname="AirPassengers",
package="datasets",
cache=True).data
mdata = pd.DataFrame(data=mdata["value"]).set_index(
pd.date_range(start="1949", end="1961", freq="M"))
``````
``````Out [2]:
value
1949-01-31    112
1949-02-28    118
1949-03-31    132
1949-04-30    129
1949-05-31    121
``````

Third, we delimit training range for model fitting as first ten years of data and store outcome within `tdata` object. Then, we delimit testing range for model forecasting as last two years of data and store outcome within `fdata` object. Notice that training and testing ranges delimiting was only included as an educational example which can be modified according to your needs.

``````In [3]:
tdata = mdata[:"1958-12-31"]
fdata = mdata["1959-01-01":]
``````

Fourth, we view training range data with `plot`, `ylabel` and `xlabel` functions. Within `plot` function, training range data object is included. Within `ylabel` and `xlabel` functions, vertical axis label and horizontal axis label strings are included.

``````In [4]:
plt.plot(tdata)
plt.ylabel("Air Passengers")
plt.xlabel("Year")
plt.show()
``````
``Out [4]:``

Fifth, we do training range time series classical additive seasonal decomposition by moving averages with `seasonal_decompose` function and store results within `tsdec` object. Within `seasonal_decompose` function, parameters `x=tdata` includes training range data object, `model="additive"` includes additive seasonal component and `two_sided=True` includes centered seasonal simple moving average estimation. Notice that we have to evaluate whether time series classical additive or multiplicative seasonal decomposition is needed. Also, notice that `seasonal_decompose` function parameters were only included as educational examples which can be modified according to your needs. Then we do time series decomposition chart with `tsdec` object `plot` method.

``````In [5]:
two_sided=True)
tsdec.plot()
plt.show()
``````
``Out [5]:``

References

[1] Data Description: Monthly international airline passenger numbers in thousands from 1949 to 1960.

Original Source: Box, G. E. P., Jenkins, G. M. and Reinsel, G. C. (1976). “Time Series Analysis, Forecasting and Control”. Third Edition. Holden-Day. Series G.

Source: datasets R Package AirPassengers Object. R Core Team (2021). “R: A language and environment for statistical computing”. R Foundation for Statistical Computing, Vienna, Austria.

[2] pandas Python package: Wes McKinney. (2010). Data Structures for Statistical Computing in Python, Proceedings of the 9th Python in Science Conference, 51-56.

statsmodels Python package: Seabold, Skipper, and Josef Perktold. (2010). “statsmodels: Econometric and statistical modeling with python”. Proceedings of the 9th Python in Science Conference.

matplotlib Python package: John D. Hunter. (2007). Matplotlib: A 2D Graphics Environment. Computing in Science & Engineering, 9, 90-95.

+