2
votes

I am trying to implement a time series forecast and followed the helpful tutorial as found here : https://www.analyticsvidhya.com/blog/2016/02/time-series-forecasting-codes-python/

A sample of my time series object, after log transformations and so on can be found below:

ts = 

2015-02-01  4.532599
2015-03-01  7.635787
2015-04-01  7.698029
2015-05-01  4.564348
2015-06-01  4.744932
2015-09-01  5.365976
2015-10-01  7.657283
2016-02-01  7.059618
2016-03-01  5.433722
2016-04-01  7.600902

When I get at the point to try and fir the AR model, i.e.

model = ARIMA(ts_log, order=(1, 1, 0))
results_AR = model.fit(disp=-1)
plt.plot(ts_log)
plt.plot(results_AR.fittedvalues, color='red')
plt.title('RSS: %.4f'% sum((results_AR.fittedvalues-ts_log_diff)**2))
plt.show()

My issue is when I try to do so I get:

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')

When I print out the type of data in my time series index I get:

DatetimeIndex(['2015-05-01'], dtype='datetime64[ns]', freq=None)

Then I tried adding the date parameter in ARIMA() like this:

model = ARIMA(dt_ts, order=(1, 1, 0), dates=dt_ts.index.values)

or by adding a separate array that has all my dates called formatted:

model = ARIMA(dt_ts, order=(1, 1, 0), dates=formatted)

And in both cases I got this:

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

Anyone has an idea why this happens and how I can fix it?

Thanks in advance.

1

1 Answers

1
votes

Looks like your index is not defined properly.

So the problem lies in the original ts.

I tried the replicate the error with the code below and it is running as expected.

import pandas as pd
from statsmodels.tsa.arima_model import ARIMA

periods = 10000
my_index = pd.date_range('2016-07-01', periods=periods, freq='D')
data = np.random.randint(100,1000,periods)
ts = pd.Series(data=data, index=my_index, name='Monthly Returns')
ts_log=np.log(ts).diff().dropna()
print(ts.index)

DatetimeIndex(['2016-07-01', '2016-07-02', '2016-07-03', '2016-07-04',
           '2016-07-05', '2016-07-06', '2016-07-07', '2016-07-08',
           '2016-07-09', '2016-07-10',
           ...
           '2043-11-07', '2043-11-08', '2043-11-09', '2043-11-10',
           '2043-11-11', '2043-11-12', '2043-11-13', '2043-11-14',
           '2043-11-15', '2043-11-16'],
          dtype='datetime64[ns]', length=10000, freq='D')

And if we do the remaining calculations, it should plot without any errors.

model = ARIMA(ts_log, order=(1, 1, 0))
results_AR = model.fit(disp=-1)
plt.plot(ts_log)
plt.plot(results_AR.fittedvalues, color='red')
plt.title('RSS: %.4f'% sum((results_AR.fittedvalues-ts_log)**2))
plt.show()

Resulting Plot