Decomposing trend, seasonal and residual time series elements

萝らか妹 提交于 2019-11-30 08:26:37

Works fine when you convert your index to DateTimeIndex:

df.reset_index(inplace=True)
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
s=sm.tsa.seasonal_decompose(df.divida)

<statsmodels.tsa.seasonal.DecomposeResult object at 0x110ec3710>

Access the components via:

s.resid
s.seasonal
s.trend
saravanan saminathan

Statsmodel will decompose the series only if you provide frequency. Usually all time series index will contain frequency eg: Daywise, Business days, weekly So it shows error. You can remove this error by two ways:

  1. What Stefan did is he gave the index column to pandas DateTime function. It uses internal function infer_freq to find the frequency and return the index with frequency.
  2. Else you can set the frequency to your index column as df.index.asfreq(freq='m'). Here m represents month. You can set the frequency if you have domain knowledge or by d.

Make it simple:

Follow three steps: 1-if not done, make the column in yyyy-mm-dd or dd-mm-yyyy( using excel). 2-Then using pandas convert it into date format as:

df['Date'] = pd.to_datetime(df['Date'])

3-decompose it using:

from statsmodels.tsa.seasonal import seasonal_decompose decomposition=seasonal_decompose(ts_log)

And finally:----

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!