【问题标题】:time series decomposition error in pythonpython中的时间序列分解错误
【发布时间】:2020-02-20 18:51:56
【问题描述】:

我有以下数据框

data = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [11558522, 12323552, 13770958, 18412280]
}) 

然后我将日期列变成日期时间数据类型

data['date'] = pd.to_datetime(data['date'],format = '%Y')

当我打印我得到的数据类型时

print(data.dtypes)

>>> Register No.
>>> date    datetime64[ns]
>>> Sum              int64
>>> dtype: object

然后我使用以下代码进行时间序列分解

from pylab import rcParams
from statsmodels.tsa.seasonal import seasonal_decompose 
rcParams ['figure.figsize'] = 18,8
decomposition = seasonal_decompose(data, model='additive', freq=30)
fig = decomposition.plot()

但我得到以下错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-e3a60d7302da> in <module>
      3 
      4 rcParams ['figure.figsize'] = 18,8
----> 5 decomposition = seasonal_decompose(data, model='additive', freq=30)
      6 
      7 fig = decomposition.plot()

~/opt/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/seasonal.py in seasonal_decompose(x, model, filt, freq, two_sided, extrapolate_trend)
    113     nobs = len(x)
    114 
--> 115     if not np.all(np.isfinite(x)):
    116         raise ValueError("This function does not handle missing values")
    117     if model.startswith('m'):

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

我的数据框中没有任何缺失值,我不确定为什么会出现此错误

【问题讨论】:

    标签: python pandas dataframe time-series


    【解决方案1】:

    您需要将date 设置为数据框中的索引:

    data = data.set_index('date')
    

    【讨论】:

    • 如果我这样做,我会遇到以下错误:ValueError:操作数无法与形状(4,)(58,)一起广播
    • 您需要在seasonal_decompose 中降低freq。或者你的时间序列应该更长。
    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多