Python: Getting KeyError while executing forcast( ) in statsmodel's holtwinters function

442 views Asked by At

I'm trying to get time series prediction using the following code.

from statsmodels.tsa.holtwinters import ExponentialSmoothing as HWES

#read the data file. the date column is expected to be in the mm-dd-yyyy format.
df_train_y = pd.DataFrame(data = tsne_train_output)
df_train_y.index.freq = 'd'

df_test_y = pd.DataFrame(data = tsne_test_output)
df_test_y.index.freq = 'd'

#plot the data
df_train_y.plot()
plt.show()


#build and train the model on the training data
model = HWES(df_train_y, seasonal_periods=144, trend='add', seasonal='add')
fitted = model.fit(optimized=True, use_brute=True)

#print out the training summary
print(fitted.summary())

#create an out of sample forcast for the next 12 steps beyond the final data point in the training data set
trend_forecast = fitted.forecast(steps= 157200)

And my data looks like this:

df_train_y >>

    y_train
0   0
1   0
2   0
3   0
4   0
... ...
366755  65
366756  66
366757  63
366758  65
366759  68

When I'm executing the above code on my data, I'm getting the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-137-66fe58a542ec> in <module>()
     23 
     24 #create an out of sample forcast for the next 12 steps beyond the final data point in the training data set
---> 25 trend_forecast = fitted.forecast(steps= '157200')

1 frames
/usr/local/lib/python3.7/dist-packages/statsmodels/tsa/holtwinters.py in forecast(self, steps)
    344         try:
    345             freq = getattr(self.model._index, 'freq', 1)
--> 346             start = self.model._index[-1] + freq
    347             end = self.model._index[-1] + steps * freq
    348             return self.model.predict(self.params, start=start, end=end)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

So I tried to check the type of operands in the line 346, upon running type(fitted.model._index[-1]) I'm getting int; also I edited the code in holtwinters.py by typecasting all operands to int using int() but still the error persists.

I have reported this issue in the github repo for statsmodels here. And asked same question in datascience stackexchange, but got nothing. DS Stackexchange question link

0

There are 0 answers