1

我有一个由时间戳索引的熊猫数据框对象。我正在尝试使用以下代码从 statsmodels 拟合 AR 模型

df = pd.read_csv('xxx')
model=tsa.ar_model.AR(df['price'])

但是我收到了错误

ValueError: dates must be of type datetime

但据我所知,日期格式正确。如果我显示打印 df['price'] 的结果也可能会有所帮助

 timestamp
1976-01-01 12:00:00     96541
1976-02-01 12:00:00     90103
1976-03-01 12:00:00     96541
1976-04-01 12:00:00    108112
1976-05-01 12:00:00    115855
1976-06-01 12:00:00    119712
1976-07-01 12:00:00    115855
1976-08-01 12:00:00    114550
1976-09-01 12:00:00    118407
1976-10-01 12:00:00    128702
1976-11-01 12:00:00    115855
1976-12-01 12:00:00    102979
1977-01-01 12:00:00    111969
1977-02-01 12:00:00    106836
1977-03-01 12:00:00    115594
...
2012-05-01 12:00:00    257375
2012-06-01 12:00:00    250850
2012-07-01 12:00:00    246500
2012-08-01 12:00:00    242150
2012-09-01 12:00:00    237452
2012-10-01 12:00:00    230724
2012-11-01 12:00:00    218950
2012-12-01 12:00:00    210250
2013-01-01 12:00:00    210250
2013-02-01 12:00:00    203000
2013-03-01 12:00:00    218950
2013-04-01 12:00:00    232000
2013-05-01 12:00:00    232000
2013-06-01 12:00:00    226548
2013-07-01 12:00:00    226548
4

1 回答 1

5

如前所述df.index.dtype,尽管您将时间戳视为日期时间,但它是一个对象。您可以轻松地将其转换为日期时间

df.index = pd.to_datetime(df.index)
于 2013-11-10T18:08:39.973 回答