2
$\begingroup$
import statsmodels.api as sm

y_hat_avg = test.copy()
fit1 = sm.tsa.statespace.SARIMAX(train.Netsales, order=(2, 1, 4),seasonal_order=(0,1,1,7)).fit()
y_hat_avg['SARIMA'] = fit1.predict(start="2017-12-06", end="2018-04-06", dynamic=True)
plt.figure(figsize=(16,8))
plt.plot( train['Netsales'], label='Train')
plt.plot(test['Netsales'], label='Test')
plt.plot(y_hat_avg['SARIMA'], label='SARIMA')
plt.legend(loc='best')
plt.show()

Getting this error when I use this code

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py:225: 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)
/anaconda3/lib/python3.6/site-packages/statsmodels/base/model.py:508: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
  "Check mle_retvals", ConvergenceWarning)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2524             try:
-> 2525                 return self._engine.get_loc(key)
   2526             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine._date_check_type()

KeyError: '2017-12-06'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
   1434         try:
-> 1435             return Index.get_loc(self, key, method, tolerance)
   1436         except (KeyError, ValueError, TypeError):

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2526             except KeyError:
-> 2527                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2528 

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine._date_check_type()

KeyError: '2017-12-06'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1512518400000000000

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2524             try:
-> 2525                 return self._engine.get_loc(key)
   2526             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

KeyError: Timestamp('2017-12-06 00:00:00')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1512518400000000000

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
   1443                 stamp = Timestamp(key, tz=self.tz)
-> 1444                 return Index.get_loc(self, stamp, method, tolerance)
   1445             except KeyError:

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2526             except KeyError:
-> 2527                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2528 

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

KeyError: Timestamp('2017-12-06 00:00:00')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_label_loc(self, key, base_index)
    415                 if not isinstance(key, (int, long, np.integer)):
--> 416                     loc = self.data.row_labels.get_loc(key)
    417                 else:

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
   1445             except KeyError:
-> 1446                 raise KeyError(key)
   1447             except ValueError as e:

KeyError: '2017-12-06'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_prediction_index(self, start, end, index, silent)
    476         try:
--> 477             start, start_index, start_oos = self._get_index_label_loc(start)
    478         except KeyError:

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_label_loc(self, key, base_index)
    422             except:
--> 423                 raise e
    424         return loc, index, index_was_expanded

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_label_loc(self, key, base_index)
    411             loc, index, index_was_expanded = (
--> 412                 self._get_index_loc(key, base_index))
    413         except KeyError as e:

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_loc(self, key, base_index)
    363             except (IndexError, ValueError) as e:
--> 364                 raise KeyError(str(e))
    365             loc = key

KeyError: "invalid literal for int() with base 10: '2017-12-06'"

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-58-0249e7d3f9ee> in <module>()
      3 y_hat_avg = test.copy()
      4 fit1 = sm.tsa.statespace.SARIMAX(train.Netsales, order=(2, 1, 4),seasonal_order=(0,1,1,7)).fit()
----> 5 y_hat_avg['SARIMA'] = fit1.predict(start="2017-12-06", end="2018-04-06", dynamic=True)
      6 plt.figure(figsize=(16,8))
      7 plt.plot( train['Netsales'], label='Train')

/anaconda3/lib/python3.6/site-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs)
     93             obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:])
     94         elif how:
---> 95             obj = data.wrap_output(func(results, *args, **kwargs), how)
     96         return obj
     97 

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/statespace/mlemodel.py in predict(self, start, end, dynamic, **kwargs)
   2432         """
   2433         # Perform the prediction
-> 2434         prediction_results = self.get_prediction(start, end, dynamic, **kwargs)
   2435         return prediction_results.predicted_mean
   2436 

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/statespace/sarimax.py in get_prediction(self, start, end, dynamic, index, exog, **kwargs)
   1924         # Handle start, end, dynamic
   1925         _start, _end, _out_of_sample, prediction_index = (
-> 1926             self.model._get_prediction_index(start, end, index, silent=True))
   1927 
   1928         # Handle exogenous parameters

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py in _get_prediction_index(self, start, end, index, silent)
    477             start, start_index, start_oos = self._get_index_label_loc(start)
    478         except KeyError:
--> 479             raise KeyError('The `start` argument could not be matched to a'
    480                            ' location related to the index of the data.')
    481         if end is None:

KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'

This is the data I am trying to train the model with

<bound method NDFrame.head of               SaleDate   Netsales
SaleDate                         
2012-12-01  01-12-2012      83.60
2012-12-04  04-12-2012   -3515.00
2012-12-05  05-12-2012   26103.25
.
.
.
.
.

Can someone please help on how to fix it, tried changing the start and end parameters but doesn't work.

$\endgroup$
4
  • 1
    $\begingroup$ Did You get any answer for this? $\endgroup$ – Amit Jun 12 '18 at 14:33
  • $\begingroup$ Have a look at the points before the traceback starts actually.. you probably need to do some conversion before applying the same, there are key errors too $\endgroup$ – Aditya Jun 12 '18 at 14:40
  • $\begingroup$ give the start and end parameter an integer number that corresponds to the starting and ending date index in your data. $\endgroup$ – Mojtaba Nov 4 '18 at 5:41
  • $\begingroup$ Try dropping the start argument. Statsmodels will then start the predictions at the first observation of the training set. $\endgroup$ – burk Jun 27 '19 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.