python - 如何解决属性错误 "Int64Index'对象没有属性 'inferred_freq'》

标签 python time-series jupyter statsmodels forecast

我正在尝试绘制季节性分解模型,以便使用给我一个错误的代码来描述时间序列趋势和季节性

AttributeError: 'Int64Index' object has no attribute 'inferred_freq"

from plotly.plotly import plot_mpl
from statsmodels.tsa.seasonal import seasonal_decompose
result = seasonal_decompose(series, model='multiplicative')
fig = result.plot()
plot_mpl(fig)

我正在关注 this link 中的模型

我的数据框看起来像这样:

    date          Name    Count
1   2018-09-04    MALX     198
2   2018-09-06    MALX     200
3   2018-09-11    MALX     203
4   2018-09-16    MALX     215
5   2018-09-17    MALX     401

我正在使用以下软件包:

import pandas as pd
import numpy as np
from pandas import Series
from matplotlib import pyplot
import matplotlib.pyplot as plt
series = pd.read_csv('Malx_data.csv', low_memory=False, usecols= ['date', 'Name', 'Count'])
series = series.loc[series['Name'] == 'MALX'].sort_values(by = 'day', ascending = True)
series['date'] = pd.to_datetime(series['date'])
series

回溯错误信息如下:

AttributeError                            Traceback (most recent call last)
<ipython-input-43-4e5d5e1e2756> in <module>()
      1 from plotly.plotly import plot_mpl
      2 from statsmodels.tsa.seasonal import seasonal_decompose
----> 3 result = seasonal_decompose(series, model='multiplicative')
      4 fig = result.plot()
      5 plot_mpl(fig)

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\statsmodels\tsa\seasonal.py in seasonal_decompose(x, model, filt, freq, two_sided, extrapolate_trend)
    106     """
    107     if freq is None:
--> 108         _pandas_wrapper, pfreq = _maybe_get_pandas_wrapper_freq(x)
    109     else:
    110         _pandas_wrapper = _maybe_get_pandas_wrapper(x)

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\statsmodels\tsa\filters\_utils.py in _maybe_get_pandas_wrapper_freq(X, trim)
     43         index = X.index
     44         func = _get_pandas_wrapper(X, trim)
---> 45         freq = index.inferred_freq
     46         return func, freq
     47     else:

AttributeError: 'Int64Index' object has no attribute 'inferred_freq'

最佳答案

看起来您正在分解的系列没有时间序列索引。您可以通过以下方式添加:

series.index = series.date

关于python - 如何解决属性错误 "Int64Index'对象没有属性 'inferred_freq'》,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55749463/

相关文章:

ipython - 在干净的 Ubuntu 14.04 LTS 上安装 Jupyter Notebook 失败

python - 如何使用 Python 将每个月的常数乘以原始时间序列

machine-learning - 使用不同长度的时间序列在 Keras 中训练 LSTM

jupyter-notebook - 如何列出JupyterLab中所有现有的工作区?

r - python 2、julia 和 R 内核已死,但 jupyter (ubuntu) 中的 python 3。怎样才能回到正常的环境?

python - 在数据框中选择时间窗口

python - 在用 Python 编写的列表中搜索列表

python - 统一码解密

python - 如何在 python 上打印彩色的特定单词?

python - Pandas :更改列中 bool 索引选择的值而不会收到警告