24

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'

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
Dorki
  • 1,021
  • 2
  • 8
  • 23
  • Is your data monthly? If so, you can do `df.index = df.index.to_period('M')`. – Quang Hoang Oct 22 '19 at 19:08
  • @QuangHoang it says that this method doesn't exists, I just did: `print(type(df.index))` and this was the output: `` hopefully this information will help – Dorki Oct 22 '19 at 19:11
  • 2
    `type(df.index)` doesn't really help :-). Next time try `df.index.dtype`. Anyway, you should only do `to_period('M')` on `DatetimeIndex` so `df.index = pd.DatetimeIndex(df.index).to_period('M')` – Quang Hoang Oct 22 '19 at 19:14

2 Answers2

35

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')
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
2

If your time series is irregular, you might end up receiving an error like this.

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

Depending on what you want to do with you data, you could try different things.
What worked for me was pandas resampling, better explained here.