Search code examples
python-3.xpandastime-seriesstl-decomposition

I'm having trouble to decompose a time-series, in python


So I have used the previous answer and question to my problems answer but in my case I am facing some error I don't know how to solve it.

Initially I have loaded a pandas data frame as df = pd.read_excel(fid_data), the content of this is checked in the next command df.info(), I get the following:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 118 entries, 0 to 117
Data columns (total 8 columns):
Date       118 non-null datetime64[ns]
MOEX       118 non-null float64
RTS        118 non-null float64
CAC40      118 non-null float64
DAX        118 non-null float64
FTSe100    118 non-null float64
nikkei     118 non-null float64
sp500      118 non-null float64
dtypes: datetime64[ns](1), float64(7)
memory usage: 7.5 KB

When I try to decompose moex = df.MOEX with this command res = sm.tsa.seasonal_decompose(moex, model='additive') I get the following error:

Traceback (most recent call last):
  File "Main.py", line 106, in <module>
    res = sm.tsa.seasonal_decompose(moex, model='additive')
  File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/seasonal.py", line 68, in seasonal_decompose
    _pandas_wrapper, pfreq = _maybe_get_pandas_wrapper_freq(x)
  File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/filters/_utils.py", line 46, in _maybe_get_pandas_wrapper_freq
    freq = index.inferred_freq
AttributeError: 'RangeIndex' object has no attribute 'inferred_freq'

Solution

  • So many thanks to @QuangHoang, after loading the pandas df object you must define the temporal scale with df.set_index('Date', inplace=True), and the variable definitions now does not contains the Date array.

    Before:

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 118 entries, 0 to 117
    Data columns (total 8 columns):
    Date       118 non-null datetime64[ns]
    MOEX       118 non-null float64
    RTS        118 non-null float64
    CAC40      118 non-null float64
    DAX        118 non-null float64
    FTSe100    118 non-null float64
    nikkei     118 non-null float64
    sp500      118 non-null float64
    dtypes: datetime64[ns](1), float64(7)
    memory usage: 7.5 KB
    

    After:

    <class 'pandas.core.frame.DataFrame'>
    DatetimeIndex: 118 entries, 2019-02-01 to 2009-05-01
    Data columns (total 7 columns):
    MOEX       118 non-null float64
    RTS        118 non-null float64
    CAC40      118 non-null float64
    DAX        118 non-null float64
    FTSe100    118 non-null float64
    nikkei     118 non-null float64
    sp500      118 non-null float64
    dtypes: float64(7)
    memory usage: 7.4 KB
    

    Everything works as expected. Now I do not need to parse the Date array, since its inserted in each array...

    Thanks again.-