Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

like image 386
Dorki Avatar asked Oct 22 '19 18:10

Dorki


1 Answers

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')
like image 123
Quang Hoang Avatar answered Oct 22 '22 05:10

Quang Hoang