在 python 中用 statsmodels建立 ARIMA 模型进行预测时间序列:python
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'))
运行后报错:api
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
的结果是:spa
DatetimeIndex(['2016-01-01', '2016-01-08', '2016-01-15', '2016-01-22', '2016-01-29'], dtype='datetime64[ns]', freq='W-FRI')
可是此时还会报错debug
KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'
这是因为get_prediction的 start 参数必须是在时间序列中出现过的时间。code
debug 经验++:使用库时,由于层层调用,有时赶上问题光看报错信息解决不了,而调用的代码又没写错,那么颇有可能就是数据的问题了。 虽然搜索引擎很好用,可是对于有些小问题来讲,可能会变成盲目地在互联网大海捞针。对于开源的库,能够看看有没有相似的别人提过的 issue ,有时候确实是库的bug。本身定位问题还有个办法是对比正确完整的例子,找不一样点。索引