Skip to content

BUG: Fix missing="drop" with a boolean endog - #10477

Merged
bashtage merged 1 commit into
statsmodels:mainfrom
shan3520:bug-5445-bool-endog-missing
Oct 19, 2026
Merged

bashtage merged 1 commit into
statsmodels:mainfrom
shan3520:bug-5445-bool-endog-missing

Conversation

@shan3520

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug where constructing an instance of any model (such as Logit, OLS) from boolean endog and missing="drop" results in a crash regardless of whether there are any NaNs in the inputs. On main, it causes ValueError: cannot reshape array ... for Pandas data and IndexError for NumPy data.

Root Cause

The underlying problem lies in base/data.py in _nan_rows. The function previously combined missing value row masks using reduce and then checked whether the running mask had a bool dtype in order to identify it. When the user-supplied boolean endog was used as the first argument to _nan_rows, it mistakenly detected a mask there instead. OR-ing of this 1D array with an (n, 1) shape mask resulted in a wrong n by n matrix because of broadcasting.

Fix

Reimplemented _nan_rows with explicit OR-ing of the row masks in a loop, without the failed check above and the unused reduce import.

Also updated one internal call site (the handling of 2D extra arrays, such as GLS sigma) that implicitly relied on the previous behavior by passing the row mask as the first argument to _nan_rows. It will now correctly compute nan_mask | _nan_rows(*combined_2d) instead.

Notes: Behavior with non-boolean data stays the same. Pandas boolean columns which have NaN values in them get cast to the object dtype, and so still result in rejection with the expected, separate error.

Testing

A unit test was added specifically to test the behavior with the first array being boolean.

Four model test cases (Logit, numpy/pandas input, with and without NaN in exog).

Tests verify that the right rows were dropped and that the parameters are consistent with a complete case fit (fail on main).

No R comparison required - testing only data dropping behavior.

Ran internal shared regression test suites base, regression, discrete, genmod, tsa, stats and many others with 19,822 tests, all passing.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: drafting the fix and tests.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

_nan_rows combined the missing masks of the arrays with reduce and
recognized the running mask by its boolean dtype. A boolean data array
in the first position was therefore used as a mask and broadcast to an
nobs x nobs array, so any model with a boolean endog failed with
missing="drop". Combine the masks explicitly and pass the mask
separately where it was combined with the 2d arrays.

closes statsmodels#5445
@shan3520
shan3520 force-pushed the bug-5445-bool-endog-missing branch from 0034acd to ce056fd Compare October 11, 2026 13:37
@shan3520

Copy link
Copy Markdown
Contributor Author

Hello @bashtage, it’s just a quick ping regarding this PR to fix #5445 (boolean endog with missing="drop" crashes any model). As this PR modifies base/data.py which is common logic, I totally get that this needs a thorough review. Please do let me know if there is anything that could help in the review process, such as adding more tests, splitting the _nan_rows change from the call-site change, etc.

@bashtage

Copy link
Copy Markdown
Member

Thanks LGTM.

We really should systematically test other numeric types, e.g., integers and bools, as inputs to make sure there aren't strange issues due to using empty_like or zeros_like and then in-place ops.

@bashtage
bashtage merged commit 408f6c0 into statsmodels:main Oct 19, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

missing='drop' returning "Exception: Data must be 1-dimensional" error for sm.Logit

2 participants