Repository navigation
BUG: Fix missing="drop" with a boolean endog - #10477
Merged
Merged
Conversation
_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
force-pushed
the
bug-5445-bool-endog-missing
branch
from
October 11, 2026 13:37
0034acd to
ce056fd
Compare
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. |
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. |
4 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
NumPy's guide.
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.
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.