Last Update: June 1, 2022
My online video tutorials are hosted at YouTube channel.
For learning this concept, you can view my online video tutorials: Time Series Decomposition: Classical Method in Python (Spyder) and Time Series Decomposition: Classical Method in Python (Jupyter).
Videos Code
1. Packages
import pandas as pd
import statsmodels.api as sm
import statsmodels.tsa.api as ts
import matplotlib.pyplot as plt
2. Data
mdata_obj = sm.datasets.get_rdataset(dataname="AirPassengers",
package="datasets",
cache=True)
mdata = mdata_obj.data
mdata = pd.DataFrame(data=mdata["value"]).set_index(
pd.date_range(start="1949", end="1961", freq="M"))
print(mdata.head())
print(mdata_obj.__doc__)
Ranges Delimiting
tdata = mdata[:"1958-12-31"]
fdata = mdata["1959-01-01":]
Chart
plt.plot(tdata)
plt.ylabel("Air Passengers")
plt.xlabel("Year")
plt.show()
3. Time Series Decomposition
Classical Method
tsdec = ts.seasonal_decompose(x=tdata, model="additive",
two_sided=True)
tsdec.plot()
plt.show()