"start"参数无法与与数据索引相关的位置匹配



我不知道为什么我的"启动"pred不起作用。我对pd.to_datetime添加了一些编辑,但它们不起作用。

这是我的代码:

pred = results.get_prediction(start=pd.to_datetime('2018-06-01'), dynamic=False)
pred_ci = pred.conf_int()
ax = y['2015':].plot(label='observed')
pred.predicted_mean.plot(ax=ax, label='One-step ahead Forecast', alpha=.7, figsize=(14, 4))
ax.fill_between(pred_ci.index,
pred_ci.iloc[:, 0],
pred_ci.iloc[:, 1], color='k', alpha=.2)
ax.set_xlabel('Date')
ax.set_ylabel('Retail_sold')
plt.legend()
plt.show()

我的错误日志总是指我的时间格式,在我开始分析数据并解决数据问题之前,我必须重新采样我以前的数据,从每日数据到每月数据,但我不知道为什么我的数据不能使用pd.todatetime读取。

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1546300800000000000
During handling of the above exception, another exception occurred:
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2896             try:
-> 2897                 return self._engine.get_loc(key)
2898             except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
KeyError: Timestamp('2019-01-01 00:00:00')
During handling of the above exception, another exception occurred:
KeyError                                  Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1546300800000000000
During handling of the above exception, another exception occurred:
KeyError                                  Traceback (most recent call last)
11 frames
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
KeyError: Timestamp('2019-01-01 00:00:00')
During handling of the above exception, another exception occurred:
KeyError                                  Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
KeyError: Timestamp('2019-01-01 00:00:00')
During handling of the above exception, another exception occurred:
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/statsmodels/tsa/base/tsa_model.py in _get_prediction_index(self, start, end, index, silent)
522             start, start_index, start_oos = self._get_index_label_loc(start)
523         except KeyError:
--> 524             raise KeyError('The `start` argument could not be matched to a'
525                            ' location related to the index of the data.')
526         if end is None:
KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'

我使用了Google Colab和Python 3.7。

有人能解决我的问题吗?

这里的根本问题是您的数据没有具有相关频率的索引,因为您的数据会跳过几天(例如从2016/2/5到2016/2/14(。

在一个类似的问题中,我的问题是我将一个列表作为值数据传递,我需要在pd系列中进行转换

data_to predict = pd.Series(imput_data, index=myIndex)

您应该用时间列设置数据集的索引,如下所示:

df["Time"] = pd.to_datetime(df['Time'], infer_datetime_format=True)
df = df.set_index(["Time"])

您可以尝试以下操作:

predictions = results.predict(start=train_data.shape[0],end=(train_data.shape[0]+test_data.shape[0]-1), dynamic=False)

最新更新