Error: ValueWarning: A date index has been provided, but it has no associated frequency information and so will be ignored when e.g. forecasting

Error: ValueWarning: A date index has been provided, but it has no associated frequency information and so will be ignored when e.g. forecasting

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

ValueWarning: A date index has been provided, but it has no associated frequency information and so will be ignored when e.g. forecasting.
' ignored when e.g. forecasting.', ValueWarning)

所以我发现了这两个问题:

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

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

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: 'Index' object has no attribute 'inferred_freq'

您当前的索引,如打印的那样,是字符串索引。您应该将其转换为 DatetimeIndex 并通过 to_period:

传递频率
df.index = pd.DatetimeIndex(df.index).to_period('M')

如果您的时间序列不规则,您最终可能会收到这样的错误。

"ValueError: Inferred frequency None from passed values does not conform to passed frequency M"

根据您想对数据执行的操作,您可以尝试不同的操作。
对我有用的是 pandas resampling, better explained here.