错误:ValueWarning:已提供日期索引,但没有相关的频率信息,因此在例如预测

问题描述 投票:0回答:1

所以我有一个包含两列的CSV文件:日期和价格,但是当我尝试在该时间序列上使用ARIMA时,遇到此错误:

ValueWarning:已提供日期索引,但没有关联频率信息等,例如预测。'例如预测。”,ValueWarning)

所以我发现了两个问题:

ValueWarning: No frequency information was provided, so inferred frequency MS will be used

https://stackoverflow.com/a/35860703

但是当我尝试运行示例中的代码(第二个链接):

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

我也收到了相同的ValueWarning,所以我尝试更改此行:

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

对此:

df.index = pd.DatetimeIndex(df.index.values, freq=df.index.inferred_freq)

但随后出现此错误:

[AttributeError:'索引'对象没有属性'inferred_freq'

python pandas time-series statsmodels arima
1个回答
1
投票

您所打印的当前索引是字符串索引。您应该将其转换为DatetimeIndex并通过to_period传递频率:

df.index = pd.DatetimeIndex(df.index).to_period('M')
© www.soinside.com 2019 - 2024. All rights reserved.