Skip to content

BUG: Drop observations with a missing categorical value in formulas - #895

Draft
bashtage wants to merge 1 commit into
mainfrom
fix-891-formula-missing-categorical
Draft

bashtage wants to merge 1 commit into
mainfrom
fix-891-formula-missing-categorical

Conversation

@bashtage

@bashtage bashtage commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Closes #891.

The formula parsers build the model matrices with formulaic's na_action="ignore", so that the models drop the observations with missing values themselves. A missing numeric value stays missing, but formulaic encodes a missing value of a categorical variable, C(g) or a column of strings, as zeros in all of its dummy variables, so the observation was kept in the base level without a warning. The same data given as arrays are dropped with a MissingValueWarning since #858. 7.1 behaves the same way as main: with the example in #891, the formula keeps 400 observations and data.dropna() 397.

  • set_missing_rows in shared/utility.py makes the rows that formulaic would drop for missing values missing in every column of a model matrix, so the models drop them as for arrays. It asks formulaic for those rows with the model spec and na_action="drop", so the state of stateful transforms such as center is learnt as before, and it only runs for formulas with a categorical variable. The rows are found by position, since formulaic raises a length error when it drops rows of data whose index has duplicate labels.
  • The IV and panel parsers and the asset pricing formulas use it. The system models and AbsorbingLS.from_formula use the IV parser.
  • predict with data treats a missing categorical value as missing too (it is left out, or NaN with missing=True), and formulaic's DataMismatchWarning about it is no longer shown.
  • A versionchanged:: 7.2 note in the Notes of the 18 from_formula methods, and a note under "Changes that alter results".

test_formula_missing_categorical.py checks that the formula gives the same estimates as the formula on the data without the rows with a missing value (rtol 1e-10), with a MissingValueWarning, for IV2SLS and IVGMM with C(g), a string column and a pandas categorical, AbsorbingLS, PooledOLS, PanelOLS with entity effects, SUR, and LinearFactorModel with and without separate portfolios, and with an index with duplicate labels, and that predict gives NaN for the row. All 14 tests fail on main. Each of these changes makes a test fail: locating the rows by label instead of position, showing the warning in predict, and leaving out either of the asset pricing calls.

The pull request with the follow-ups to the merged 7.2 pull requests (#896) also adds a test for #884. The two do not conflict, and the path of model_matrix_from_spec for a missing value without a new level, which codecov reported on #884, is tested here, since this change alters it.

The whole suite passes (pip-pre: Python 3.14, NumPy and pandas development versions), with no new warnings.

🤖 Generated with Claude Code

The formula parsers build the model matrices with formulaic's
na_action="ignore", so that the models drop the observations with missing
values themselves. A missing numeric value stays missing, but formulaic
encodes a missing value of a categorical variable, C(g) or a column of
strings, as zeros in all of its dummy variables, so the observation was
kept in the base level without a warning. The same data given as arrays
are dropped with a MissingValueWarning since #858.

The parsers of the IV, panel and asset pricing models, which the system
models and AbsorbingLS use too, now make the rows that formulaic would
drop for missing values missing in every column, so that the models drop
them as for arrays. The state of stateful transforms is learnt as before.
The rows are found by position, since formulaic cannot drop rows of data
whose index has duplicate labels. predict with data treats a missing
categorical value as missing as well, and formulaic's warning about it is
no longer shown.

Closes #891
@bashtage
bashtage force-pushed the fix-891-formula-missing-categorical branch from 028fd46 to 38b39c0 Compare December 3, 2026 11:02
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.69%. Comparing base (1e04074) to head (38b39c0).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main     #895    +/-   ##
========================================
  Coverage   99.69%   99.69%            
========================================
  Files         206      207     +1     
  Lines       39413    39518   +105     
  Branches     2537     2542     +5     
========================================
+ Hits        39291    39399   +108     
+ Misses         52       51     -1     
+ Partials       70       68     -2     
Flag Coverage Δ
adder 99.69% <100.00%> (+<0.01%) ⬆️
subtractor 99.69% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

Formulas keep observations with a missing categorical value as the base level

1 participant