Missing Data#
All of the models can handle missing data. For performance reasons, the default is not to do any checking for missing data. If, however, you would like for missing data to be handled internally, you can do so by using the missing keyword argument. The default is to do nothing
In [1]: import statsmodels.api as sm
In [2]: data = sm.datasets.longley.load()
In [3]: data.exog = sm.add_constant(data.exog)
# add in some missing data
In [4]: missing_idx = np.array([False] * len(data.endog))
In [5]: missing_idx[[4, 10, 15]] = True
In [6]: data.endog[missing_idx] = np.nan
The linear regression models, such as OLS, WLS and GLS, check the data and raise a MissingDataError if endog, exog, weights or sigma contain NaN or infinite values, so fitting one of them to this data with the default setting fails. Other models do not check the data. They can fail with an error that does not mention the missing values, or silently return model parameters that are all NaN, which is probably not what you expected. If you are not sure whether or not you have missing data you can use missing = ‘raise’. This will raise a MissingDataError during model instantiation if missing data is present so that you know something was wrong in your input data.
In [7]: ols_model = sm.OLS(data.endog, data.exog, missing='raise')
---------------------------------------------------------------------------
MissingDataError Traceback (most recent call last)
Cell In[7], line 1
----> 1 ols_model = sm.OLS(data.endog, data.exog, missing='raise')
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/regression/linear_model.py:1112, in OLS.__init__(self, endog, exog, missing, hasconst, **kwargs)
1110 warnings.warn(msg, ValueWarning, stacklevel=2)
1111 self.offset: Float64Array | None = None # set by _handle_data if passed
-> 1112 super().__init__(endog, exog, missing=missing, hasconst=hasconst, **kwargs)
1113 if "weights" in self._init_keys:
1114 self._init_keys.remove("weights")
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/regression/linear_model.py:900, in WLS.__init__(self, endog, exog, weights, missing, hasconst, **kwargs)
898 else:
899 weights = weights.squeeze()
--> 900 super().__init__(
901 endog, exog, missing=missing, weights=weights, hasconst=hasconst, **kwargs
902 )
903 nobs = self.exog.shape[0]
904 weights = self.weights
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/regression/linear_model.py:256, in RegressionModel.__init__(self, endog, exog, **kwargs)
254 self.weights: Float64Array | None = None
255 self.sigma: Float64Array | None = None
--> 256 super().__init__(endog, exog, **kwargs)
257 self._data_attr.extend(["pinv_wexog", "wendog", "wexog", "weights"])
258 self._check_finite()
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/base/model.py:314, in LikelihoodModel.__init__(self, endog, exog, **kwargs)
313 def __init__(self, endog, exog=None, **kwargs):
--> 314 super().__init__(endog, exog, **kwargs)
315 self.initialize()
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/base/model.py:102, in Model.__init__(self, endog, exog, **kwargs)
100 missing = kwargs.pop("missing", "none")
101 hasconst = kwargs.pop("hasconst", None)
--> 102 self.data = self._handle_data(endog, exog, missing, hasconst, **kwargs)
103 self.k_constant = self.data.k_constant
104 self.exog = self.data.exog
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/base/model.py:143, in Model._handle_data(self, endog, exog, missing, hasconst, **kwargs)
142 def _handle_data(self, endog, exog, missing, hasconst, **kwargs):
--> 143 data = handle_data(endog, exog, missing, hasconst, **kwargs)
144 # kwargs arrays could have changed, easier to just attach here
145 for key in kwargs:
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/base/data.py:756, in handle_data(endog, exog, missing, hasconst, **kwargs)
753 exog = np.asarray(exog)
755 klass = handle_data_class_factory(endog, exog)
--> 756 return klass(endog, exog=exog, missing=missing, hasconst=hasconst, **kwargs)
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/base/data.py:71, in ModelData.__init__(self, endog, exog, missing, hasconst, **kwargs)
69 self.formula = kwargs.pop("formula")
70 if missing != "none":
---> 71 arrays, nan_idx = self.handle_missing(endog, exog, missing, **kwargs)
72 self.missing_row_idx = nan_idx
73 self.__dict__.update(arrays) # attach all the data arrays
File /opt/hostedtoolcache/Python/3.14.10/x64/lib/python3.14/site-packages/statsmodels/base/data.py:315, in ModelData.handle_missing(cls, endog, exog, missing, **kwargs)
312 return combined, []
314 elif missing == "raise":
--> 315 raise MissingDataError("NaNs were encountered in the data")
317 elif missing == "drop":
318 if np.all(nan_mask):
MissingDataError: NaNs were encountered in the data
If you want statsmodels to handle the missing data by dropping the observations, use missing = ‘drop’.
In [8]: ols_model = sm.OLS(data.endog, data.exog, missing='drop')
We are considering adding a configuration framework so that you can set the option with a global setting.