【问题标题】:TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“时间戳”
【发布时间】:2019-03-02 05:27:03
【问题描述】:

我正在运行一个时间序列模型,为了得到预测,我得到了错误

TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“时间戳”

输入代码:

pred = results.get_prediction(start=pd.to_datetime("2018-09-01"), dynamic=False)

输出:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-f3c669d7c5dd> in <module>()
----> 1 pred = results.get_prediction(start=pd.to_datetime("2018-09-01"), dynamic=False)

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/statespace/sarimax.py in get_prediction(self, start, end, dynamic, index, exog, **kwargs)
   1924         # Handle start, end, dynamic
   1925         _start, _end, _out_of_sample, prediction_index = (
-> 1926             self.model._get_prediction_index(start, end, index, silent=True))
   1927 
   1928         # Handle exogenous parameters

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_prediction_index(self, start, end, index, silent)
    475         # indexes.
    476         try:
--> 477             start, start_index, start_oos = self._get_index_label_loc(start)
    478         except KeyError:
    479             raise KeyError('The `start` argument could not be matched to a'

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_label_loc(self, key, base_index)
    410         try:
    411             loc, index, index_was_expanded = (
--> 412                 self._get_index_loc(key, base_index))
    413         except KeyError as e:
    414             try:

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_loc(self, key, base_index)
    351             # RangeIndex)
    352             try:
--> 353                 index[key]
    354             # We want to raise a KeyError in this case, to keep the exception
    355             # consistent across index types.

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/range.py in __getitem__(self, key)
    496 
    497         if is_scalar(key):
--> 498             n = int(key)
    499             if n != key:
    500                 return super_getitem(key)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'

但是当我在虚拟数据集上运行它时,它工作正常,但在我的数据集上。我不知道问题出在哪里。

【问题讨论】:

  • 如果你只是删除pd.to_datetime会发生什么?
  • KeyError: 'start 参数无法匹配到与数据索引相关的位置。'这是我删除 pd.to_datetime 并将其作为字符串传递时得到的错误。

标签: python time-series forecasting arima


【解决方案1】:

我在预测月度数据(季节=12)时遇到了同样的问题。 09/2018 月份表示为“2018-09-01”(包括 01 作为日)。我的(为您缩短)数据如下所示:

请注意,我的索引是日期时间索引,但无法识别它是每月数据的频率。 我解决问题的方法是使用 pandas 重新生成索引:

test = test.sort_index() #mine was not always sorted
test.index = (pd.date_range(start=test.index[0], end=test.index[-1], freq='MS')) #MS = month start, which means that the day is set to 01

然后每月频率被识别:

在那之后我没有得到

TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“时间戳”

在使用带有时间戳的 results.predict() 或 results.get_prediction() 时不再使用。 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-06-27
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2018-03-17
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多