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

Dorki picture Dorki · Oct 22, 2019 · Viewed 7.5k times · Source

So I have a CSV file with two columns: date and price, but when I tried to use ARIMA on that time series I encountered this error:

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)

so I found these two questions:

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

https://stackoverflow.com/a/35860703

but when I tried to run the code in the example ( the 2nd 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

I also received the same ValueWarning, so I tried to change this line:

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

to this:

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

but then I get this error:

AttributeError: 'Index' object has no attribute 'inferred_freq'

Answer

Quang Hoang picture Quang Hoang · Oct 22, 2019

You current index, as printed, is string index. You should convert it to DatetimeIndex and pass a frequency by to_period:

df.index = pd.DatetimeIndex(df.index).to_period('M')