我在 python 中分解时间序列时遇到问题
I'm having trouble to decompose a time-series, in python
所以我使用了之前的答案和问题来解决我的问题answer,但在我的情况下,我遇到了一些错误,我不知道如何解决它。
最初我将 pandas
数据框加载为 df = pd.read_excel(fid_data)
,其内容在下一个命令 df.info()
中检查,我得到以下内容:
<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
当我尝试使用此命令 res = sm.tsa.seasonal_decompose(moex, model='additive')
分解 moex = df.MOEX
时,出现以下错误:
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'
非常感谢@QuangHoang,在加载 pandas df
对象后,您必须使用 df.set_index('Date', inplace=True)
定义时间尺度,并且变量定义现在不包含 Date
数组。
之前:
<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
之后:
<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
一切正常。现在我不需要解析 Date 数组,因为它已插入到每个数组中...
再次感谢。-
所以我使用了之前的答案和问题来解决我的问题answer,但在我的情况下,我遇到了一些错误,我不知道如何解决它。
最初我将 pandas
数据框加载为 df = pd.read_excel(fid_data)
,其内容在下一个命令 df.info()
中检查,我得到以下内容:
<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
当我尝试使用此命令 res = sm.tsa.seasonal_decompose(moex, model='additive')
分解 moex = df.MOEX
时,出现以下错误:
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'
非常感谢@QuangHoang,在加载 pandas df
对象后,您必须使用 df.set_index('Date', inplace=True)
定义时间尺度,并且变量定义现在不包含 Date
数组。
之前:
<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
之后:
<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
一切正常。现在我不需要解析 Date 数组,因为它已插入到每个数组中...
再次感谢。-