【问题标题】:Fitting ARMA model to time series indexed by time in python将ARMA模型拟合到python中按时间索引的时间序列
【发布时间】:2016-06-21 23:37:09
【问题描述】:

我正在尝试将 ARMA 模型拟合到存储在 pandas 数据框中的时间序列。数据框有一列名为“val”的 numpy.float64 类型的值和一个 pandas 时间戳索引。时间戳采用“年-月-日时:分:秒”格式。我理解以下代码:

from statsmodels.tsa.arima_model import ARMA
model = ARMA(df["val"], (1,0))

给我错误信息:

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

因为我没有正确格式化时间戳。如何索引我的数据帧,以便 ARMA 方法接受它,同时保留我的日期和时间信息?

【问题讨论】:

  • index Datetimeindex 吗?您可以查看print df.index
  • 您的回答让我发现了我的代码中的一个错误。非常感谢!!!!!!

标签: python pandas statistics time-series statsmodels


【解决方案1】:

我认为您需要将index 转换为DatetimeIndex

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

示例:

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
<statsmodels.tsa.arima_model.ARMA object at 0x000000000D5247B8>

【讨论】:

    猜你喜欢
    • 2016-08-10
    • 2020-01-03
    • 2023-03-19
    • 2011-05-01
    • 2015-08-09
    • 2020-11-20
    • 2013-05-01
    • 2016-05-06
    • 2021-07-04
    相关资源
    最近更新 更多