Skip to main content
 首页 » 编程设计

python之来自 statsmodels 的 convolution_filter 调用的 TypeError

2024年10月17日10del

我正在努力使用 statsmodels 的 tsa.seasonal_decompose 函数进行时间序列分解,该函数调用 convolution_filter 函数并引发以下类型错误:

TypeError: 'numpy.float64' object cannot be interpreted as an index

例如,当我运行下面的代码时(由 StatsModels website 提供):

import statsmodels.api as sm 
 
dta = sm.datasets.co2.load_pandas().data 
# deal with missing values. see issue 
dta.co2.interpolate(inplace=True) 
 
res = sm.tsa.seasonal_decompose(dta.co2) 
res.plot() 

我得到以下堆栈跟踪:

--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-11-b43f2eda010e> in <module>() 
      5 dta.co2.interpolate(inplace=True) 
      6  
----> 7 res = sm.tsa.seasonal_decompose(dta.co2) 
      8 res.plot() 
 
.../statsmodels/tsa/seasonal.pyc in seasonal_decompose(x, model, filt, freq) 
     86             filt = np.repeat(1./freq, freq) 
     87  
---> 88     trend = convolution_filter(x, filt) 
     89  
     90     # nan pad for conformability - convolve doesn't do it 
 
.../statsmodels/tsa/filters/filtertools.pyc in convolution_filter(x, filt, nsides) 
    301                 result[:, i] = signal.convolve(x[:, i], np.r_[0, filt[:, i]], 
    302                                                mode='valid') 
--> 303     result = _pad_nans(result, trim_head, trim_tail) 
    304     if _pandas_wrapper: 
    305         return _pandas_wrapper(result) 
 
.../statsmodels/tsa/filters/filtertools.pyc in _pad_nans(x, head, tail) 
     26             return x 
     27         elif head and tail: 
---> 28             return np.r_[[np.nan] * head, x, [np.nan] * tail] 
     29         elif tail is None: 
     30             return np.r_[[np.nan] * head, x] 
 
TypeError: 'numpy.float64' object cannot be interpreted as an index 

我试过其他几个例子,在 Numpy 的 _pad_nans 函数上遇到了同样的问题。我正在使用 Numpy 1.12.0 和 StatsModels 0.6.1。

有人知道发生了什么吗?

请您参考如下方法:

今天早上我遇到了这个完全相同的错误。该问题是由于 Numpy 1.12.0 和 StatsModels 0.6.1 之间的不兼容造成的。它最初显示为记录的警告 here .

好消息是它已在 StatsModels 0.8 的候选版本中得到修复。不幸的是 0.8 还没有发布,也没有出现在通常的存储库中。我通过查看 github 的源代码并使用说明 here 自己构建它来让它工作。 .