怎么用python建立arma模型,将ARMA模型拟合到python中按时间索引的时间序列

I am trying to fit an ARMA model to a time series stored in a pandas dataframe. The dataframe has one column of values of type numpy.float64 named "val" and an index of pandas timestamps. The timestamps are in the "Year-Month-Day Hour:Minute:Second" format. I understand that the following code:

from statsmodels.tsa.arima_model import ARMA

model = ARMA(df["val"], (1,0))

gives me the error message:

ValueError: Given a pandas object and the index does not contain dates

because I have not formatted the timestamps correctly. How can I index my dataframe so that the ARMA method accepts it while retaining my date and time information?

解决方案

I think you need convert index to DatetimeIndex:

df.index = pd.DatetimeIndex(df.index)

Sample:

import pandas as pd

from statsmodels.tsa.arima_model import ARMA

df=pd.DataFrame({"val": pd.Series([1.1,1.7,8.4 ],

index=['2015-01-15 12:10:23','2015-02-15 12:10:23','2015-03-15 12:10:23'])})

print df

val

2015-01-15 12:10:23 1.1

2015-02-15 12:10:23 1.7

2015-03-15 12:10:23 8.4

print df.index

Index([u'2015-01-15 12:10:23',u'2015-02-15 12:10:23',u'2015-03-15 12:10:23'], dtype='object')

df.index = pd.DatetimeIndex(df.index)

print df.index

DatetimeIndex(['2015-01-15 12:10:23', '2015-02-15 12:10:23',

'2015-03-15 12:10:23'],

dtype='datetime64[ns]', freq=None)

model = ARMA(df["val"], (1,0))

print model

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值