Decomposition elements, seasonal and residual time series

I have a DataFrame with multiple time series:

  divida movav12 var varmovav12 Date 2004-01 0 NaN NaN NaN 2004-02 0 NaN NaN NaN 2004-03 0 NaN NaN NaN 2004-04 34 NaN inf NaN 2004-05 30 NaN -0.117647 NaN 2004-06 44 NaN 0.466667 NaN 2004-07 35 NaN -0.204545 NaN 2004-08 31 NaN -0.114286 NaN 2004-09 30 NaN -0.032258 NaN 2004-10 24 NaN -0.200000 NaN 2004-11 41 NaN 0.708333 NaN 2004-12 29 24.833333 -0.292683 NaN 2005-01 31 27.416667 0.068966 0.104027 2005-02 28 29.750000 -0.096774 0.085106 2005-03 27 32.000000 -0.035714 0.075630 2005-04 30 31.666667 0.111111 -0.010417 2005-05 31 31.750000 0.033333 0.002632 2005-06 39 31.333333 0.258065 -0.013123 2005-07 36 31.416667 -0.076923 0.002660 

I want to expand the first time series of divida in such a way that I can separate its trend from its seasonal and residual components.

I found the answer here , and I'm trying to use the following code:

 import statsmodels.api as sm s=sm.tsa.seasonal_decompose(divida.divida) 

However, I keep getting this error:

 Traceback (most recent call last): File "/Users/Pred_UnBR_Mod2.py", line 78, in <module> s=sm.tsa.seasonal_decompose(divida.divida) File "/Library/Python/2.7/site-packages/statsmodels/tsa/seasonal.py", line 58, in seasonal_decompose _pandas_wrapper, pfreq = _maybe_get_pandas_wrapper_freq(x) File "/Library/Python/2.7/site-packages/statsmodels/tsa/filters/_utils.py", line 46, in _maybe_get_pandas_wrapper_freq freq = index.inferred_freq AttributeError: 'Index' object has no attribute 'inferred_freq' 

Can anyone shed light on him?

+8
source share
2 answers

Works great when converting 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 to components through:

 s.resid s.seasonal s.trend 
+15
source

Statsmodel will decompose a series only if you provide a frequency. Typically, all time series indices will contain a frequency, for example: Daywise, Business days, weekly, so it shows an error. You can remove this error in two ways:

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

All Articles