Trouble forecasting in statsmodels vector_ar with an exogenous variable
07:05 27 Jan 2026

I'm struggling with incorporating an exogenous variable into a vector autoregression. Here' a a minimum working example to show the issue (not really the data I'm working with):

import pandas as pd
from statsmodels.tsa.api import VAR
import yfinance as yf

df = yf.Ticker("MSFT").history(
    start='2025-11-03', end='2025-12-24'
    )['Close'].rename('MSFT').to_frame().join(
        yf.Ticker("AAPL").history(start='2025-11-03', end='2025-12-24'
                                  )['Close'].rename('AAPL')).diff().dropna()

cdown = pd.Series(index=df.index,
                  data=[(df.index[-1]-d).days+5 for d in df.index],
                  name='Countdown')

model = VAR(df, exog=cdown)
results = model.fit(maxlags=5)
new = results.forecast(df.tail(results.k_ar), 1,
                       exog_future=pd.Series([cdown.iloc[-1]-1]))

The error message reads:

KeyError                                  Traceback (most recent call last)
File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\indexes\base.py:3812, in Index.get_loc(self, key)
   3811 try:
-> 3812     return self._engine.get_loc(casted_key)
   3813 except KeyError as err:

File pandas/_libs/index.pyx:167, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/index.pyx:196, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:7088, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:7096, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: -1

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[143], line 1
----> 1 new = results.forecast(df.tail(results.k_ar), 1,
      2                        exog_future=pd.Series([cdown.iloc[-1]-1]))

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\statsmodels\tsa\vector_ar\var_model.py:1176, in VARProcess.forecast(self, y, steps, exog_future)
   1174 else:
   1175     exog_future = np.column_stack(exogs)
-> 1176 return forecast(y, self.coefs, trend_coefs, steps, exog_future)

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\statsmodels\tsa\vector_ar\var_model.py:255, in forecast(y, coefs, trend_coefs, steps, exog)
    251 for i in range(1, p + 1):
    252     # slightly hackish
    253     if h - i <= 0:
    254         # e.g. when h=1, h-1 = 0, which is y[-1]
--> 255         prior_y = y[h - i - 1]
    256     else:
    257         # e.g. when h=2, h-1=1, which is forcs[0]
    258         prior_y = forcs[h - i - 1]

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\frame.py:4113, in DataFrame.__getitem__(self, key)
   4111 if self.columns.nlevels > 1:
   4112     return self._getitem_multilevel(key)
-> 4113 indexer = self.columns.get_loc(key)
   4114 if is_integer(indexer):
   4115     indexer = [indexer]

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\indexes\base.py:3819, in Index.get_loc(self, key)
   3814     if isinstance(casted_key, slice) or (
   3815         isinstance(casted_key, abc.Iterable)
   3816         and any(isinstance(x, slice) for x in casted_key)
   3817     ):
   3818         raise InvalidIndexError(key)
-> 3819     raise KeyError(key) from err
   3820 except TypeError:
   3821     # If we have a listlike key, _check_indexing_error will raise
   3822     #  InvalidIndexError. Otherwise we fall through and re-raise
   3823     #  the TypeError.
   3824     self._check_indexing_error(key)

KeyError: -1

I think the error has to do with how VARProcess.forecast feeds into forecast , and maybe the shape of trend_coefs isn't what it's expecting, but it isn't clear to me how I ought to be inputting the relevant data in order to get the forecast to work.

python statsmodels