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

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

原文地址:https://www.cnblogs.com/flipped/p/10290332.html

时间: 2024-08-10 15:08:13

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

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

python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io import BytesIO as StringIO   Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

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

在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete' 解决方法: 在连接外键时加上: on_delete=models.CASCADE 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument

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

渲染模板时,访问页面提示TypeError: 'UnboundField' object is not callable 检查代码,发现实例化表单类是,没有加括号:form = NewNoteForm,加了括号后就解决了form = NewNoteForm() @app.route('/index')def index(): form = NewNoteForm notes = Note.query.all() return render_template('index.html', notes

在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval

在vue-cli中使用axios拿取数据时 export default { data(){ return{ lists:{ } } }, methods: { getList(){ axios.get('https://cnodejs.org/api/v1/topics',{ }) .then(function(response){ window.console.log(response.data.data); this.lists = response.data.data; }) .catc

在使用json.dumps()格式化响应数据时报错TypeError: Object of type Response is not JSON serializable

今天在处理接口返回数据格式化的时候报错:TypeError: Object of type Response is not JSON serializable.响应的对象不可序列化 解决: 打印出它响应结果是什么类型,发现是个对象. 然后先把响应结果转为json,再去格式化响应内容. 如下: import requests import json url = 'https://api.apishop.net/common/weather/get15DaysWeatherByArea' apike

Django2.0在models创建数据表时报错

Django2.0在models创建数据表时报错TypeError: __init__() missing 1 required positional argument: 'on_delete' 如下创建了两个表,Tag 表以 Contact 表为外部键,此时可以看到pycharm控制台报错 123456789101112131415 class (models.Model): name = models.CharField(max_length=200) age = models.Intege

【python3】 django2.0 在生成数据库表时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete'

python: 3.6.4 django: 2.0 models.py 代码如下 # coding: utf-8 from django.db import models from django.contrib.auth.models import User # Create your models here. class Category(models.Model): name = models.CharField(max_length=100) class Tag(models.Model)

spark执行源码中的例子时报错

在运行spark源码时报错: Error:(45, 66) not found: type SparkFlumeProtocol  val transactionTimeout: Int, val backOffInterval: Int) extends SparkFlumeProtocol with Logging {                                                                 ^ ... Error:(25, 27) no

MyBatis查询传一个参数时报错:There is no getter for property named 'sleevetype' in 'class java.lang.Integer

用MyBatis进行查询,传入参数只有一个时(非Map)如int,报错 There is no getter for property named 'sleevetype' in 'class java.lang.Integer 原因在于测试条件写法有误, <if test="sleevetype==0"><!-- 专属 --> exclusive=1 </if> <if test="sleevetype!=0">&l