在 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。自己定位问题还有个办法是对比正确完整的例子,找不同点。

statsmodels.tsa.arima_model预测时报错TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'的更多相关文章

  1. Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

    python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io ...

  2. Ajax上传文件/照片时报错TypeError :Illegal invocation

    问题 Ajax上传文件/照片时报错TypeError :Illegal invocation 解决 网上搜索问题,错误原因可能有以下几个,依次检查: 请求类型有误,如post请求,但在后台设置的是ge ...

  3. 关于TypeError: strptime() argument 1 must be str, not bytes解析

    关于TypeError: strptime() argument 1 must be str, not bytes解析   在使用datetime.strptime(s,fmt)来输出结果日期结果时, ...

  4. pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode"

    pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode" 经查询, 看到 ...

  5. 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...

  6. Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional argument: 'on_delete'

    原因 执行命令 python manage.py makemigrations 报错 TypeError: __init__() missing 1 required positional argum ...

  7. moviepy执行TextClip.search方法时报错TypeError: a bytes-like object is required, not str

    ☞ ░ 前往老猿Python博文目录 ░ 执行TextClip.search方法时,报错: >>> from moviepy.editor import * >>> ...

  8. Django关联数据库时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    sgrade = models.ForeignKey("Grades",) 执行python manage.py makemigrations后出现TypeError: __ini ...

  9. flask渲染模板时报错TypeError: 'UnboundField' object is not callable

    渲染模板时,访问页面提示TypeError: 'UnboundField' object is not callable 检查代码,发现实例化表单类是,没有加括号:form = NewNoteForm ...

随机推荐

  1. DevExpress AspxGridView分页使用隐藏系统默认英文分页

    1第一篇文章研究了怎么汉化,但是在实际使用过程中发现汉化的有小问题,DevExpress支持自定义按钮,也可以在属性中设置成中文,这样避免汉化不准确的问题 <dx:ASPxGridView ID ...

  2. asp.net mvc前台显示带htm标签的解决办法(Razor —@Html.Raw())

    数据是从后台富文本编辑后丢在数据库后取出的,不加Html.Raw(),前台就会把Html标签一同显示

  3. Tkinter小技巧:如何为窗口右上角的‘x’添加一个自定义的响应函数

    不废话,直接上代码 import tkinter as tk from tkinter import messagebox main_window = tk.Tk() main_window.geom ...

  4. QQ登录的那些坑

    这几天在项目上面实现qq登录的功能,当功能做好后发现,同一个qq号登录之后腾讯返回的openid并不一样....(天啦噜啊~)然后查询文档以及咨询客服才知道注册申请时是有一个固定的套路的(不得不说,如 ...

  5. GZIP压缩与解压

    public class GZIP { /** * 字符串的压缩 * * @param str * 待压缩的字符串 * @return 返回压缩后的字符串 * @throws IOException ...

  6. C语言面试基础知识整理

    一.预处理 1.什么是预编译?何时需要预编译? (1)预编译又称预处理,是做些代码文本的替换工作,即程序执行前的一些预处理工作.主要处理#开头的指令,如拷贝#include包含的文件代码.替换#def ...

  7. Linux学习历程——Centos 7 diff命令

    一.命令介绍 diff命令用于比较文本差异. diff以逐行的方式,比较文本文件的异同处.如果指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录. ------------- ...

  8. js 学习之路9:运算符

    1. 算数运算符 运算符 描述 例子 结果 + 加 x=y+2 x=7 - 减 x=y-2 x=3 * 乘 x=y*2 x=10 / 除 x=y/2 x=2.5 % 求余数 (保留整数) x=y%2 ...

  9. CG-CTF simple-machine

    运行一下,输入flag: 用ida打开: input_length和input_byte_804B0C0为重命名的变量:现在一个个看调用的函数. sub_8048526(): 这个函数使用了mmap分 ...

  10. win10怎么进入和退出安全模式?

    在Win10系统里同时按下”Win+R“组合按键,在打开的运行对话框里输入命令:msconfig,然后点击确定,如下图所示. 点击打开系统配置窗口,选择引导选项卡,如下图所示. 在引导选项卡窗口下,将 ...