Multivariate VAR model: ValueError: x already contains a constant
It seems you may have things working, but maybe I can still help

Catching those pesky non-varying features

Assuming a is your dataframe, with only numerical types and columns as features, you can try the following, which will return rows filled with NaN values for features of zero variance i.e. the mean value is equal to the minimum value, so the value cannot vary.

a.T.where(a.T['mean'] == a.T['min'])


They check is slightly differently in the source code of the VAR model which raises your error:

result = (np.ptp(s) == 0.0 and np.any(s != 0.0))


So they check that the value doesn't have any peaks (ptp = "peak-to-peak"), so it doesn't vary - and also that the value is not equal to zero.

In any case, there is little tolerance for it. You have done right by trying to prune out features with low (near zero) variance.

Another possiblity which I didn't notice you having already tried, would be to utilise the other arguments to the fit() method of the VAR model class. It has the following signature:

VAR.fit(maxlags=None, method='ols', ic=None, trend='c', verbose=False)


If you were to change the trend argument to nc, meaning no constant - you may also get around your error. Check out the documentation.

Source code analysis

Looking through the contents of the file that raised your error, it seems it occurs while processing the "original input data" - as defined by the function add_trend (where your error was raised).

This suggest that your reasoning about the error being found on the data after lagged features are produced would be incorrect in this case (although it's a clever idea!)

Clearing my name...

You said:


  ... So the original answer suggested in the previous question was not entirely sufficient.


I would like to point out that my answer to your linked question was indeed sufficient, because I had written that there are constants - not that the values must all be equal to zero!  :-P