zoukankan      html  css  js  c++  java
  • statsmodels.tsa.arima_model预测时报错TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'

    在 python 中用 statsmodels创建 ARIMA 模型进行预测时间序列:

    import pandas as pd
    import statsmodels.api as sm
    
    df = pd.read_csv("data.csv", index_col=0, parse_dates=True)
    
    mod = sm.tsa.statespace.SARIMAX(df['price'], enforce_stationarity=False, enforce_invertibility=False)
    
    res = mod.fit()
    res.get_prediction(start=pd.to_datetime('2018-1-1'))
    
    

    运行后报错:

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

    这种情况的原因是,读入的时间序列数据的时间没有统一的间隔,例如打印mod._index的结果是

    DatetimeIndex(['2016-01-01', '2016-01-08', '2016-01-15', '2016-01-22',
                   '2016-01-30'],
                  dtype='datetime64[ns]', name='date', freq=None)
    

    其中2016-01-30是距离前一个时间8天,其它间隔为7天。可以看到这个 DatetimeIndex 的 freq 是 None 类型。
    而如果将最后一天修改为2016-01-29,那么mod._index的结果是:

    DatetimeIndex(['2016-01-01', '2016-01-08', '2016-01-15', '2016-01-22',
                   '2016-01-29'],
                  dtype='datetime64[ns]', freq='W-FRI')
    

    但是此时还会报错

    KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'
    

    这是由于get_prediction的 start 参数必须是在时间序列中出现过的时间。

    debug 经验++:使用库时,因为层层调用,有时遇上问题光看报错信息解决不了,而调用的代码又没写错,那么很有可能就是数据的问题了。 虽然搜索引擎很好用,但是对于有些小问题来说,可能会变成盲目地在互联网大海捞针。对于开源的库,可以看看有没有类似的别人提过的 issue ,有时候确实是库的bug。自己定位问题还有个办法是对比正确完整的例子,找不同点。

  • 相关阅读:
    人生无常 淡然处之
    对PHP开发的认知
    专家路线
    很少接触的文学
    懒加载
    Markdown入门 学习
    (转载)iOS开发历程书籍推荐
    ObjectiveC1基础代码——类和对象
    day11基础代码——函数指针
    day6
  • 原文地址:https://www.cnblogs.com/flipped/p/10290332.html
Copyright © 2011-2022 走看看