Casting an ARMA model to time indexed time series in python

I am trying to fit an ARMA model to a time series stored in a pandas data frame. The dataframe has a single column of numpy.float64 values ​​named "val" and a pandas timestamp index. Timestamps are in the format "Year-Month-Day Hour: Minute: Second". I understand that the following code:

from statsmodels.tsa.arima_model import ARMA
model = ARMA(df["val"], (1,0))

displays an error message:

ValueError: Given a pandas object and the index does not contain dates

because I did not format the timestamps correctly. How can I index my framework so that the ARMA method accepts it, storing date and time information?

+4
source share
1 answer

, index DatetimeIndex:

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

:

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
<statsmodels.tsa.arima_model.ARMA object at 0x000000000D5247B8>
+3

All Articles