Skip to content

BUG: Preserve missing categorical observations in dummy conversion - #858

Open
jackiectl2 wants to merge 1 commit into
bashtage:mainfrom
jackiectl2:fix-categorical-missing-data
Open

jackiectl2 wants to merge 1 commit into
bashtage:mainfrom
jackiectl2:fix-categorical-missing-data

Conversation

@jackiectl2

Copy link
Copy Markdown

Summary

Preserve missing categorical observations when converting IV and panel inputs to dummy variables. Missing categories currently become all-zero rows and are treated as observations of the reference category. Keep those dummy rows missing so the existing model missing-data handling can remove them. Nonmissing category coding is unchanged.

Validation

Regression before: 4 failures. After: 212 tests pass in the IV and panel data test modules. Ruff, Black, numpydoc lint on changed production modules, and git diff --check pass. The full model suite was not run.

Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com>

@bashtage bashtage left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall a good idea.

Looks a bit risky in the order that it is in. Likely an easy fix to just drop all 0 columns if s.hasnans. Is hasnans old pandas, or newer? Might have some backward compat problems, but CI will tell.

Comment thread linearmodels/iv/data.py
if isinstance(s.dtype, pd.CategoricalDtype):
out = pd.get_dummies(s, drop_first=drop_first)
if s.hasnans:
out = out.astype(float).where(s.notna(), axis=0)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks problematic with the next line. If something is dropped, the columns won't match. How about just checking if any dummies are all 0 and then dropping them?

if isinstance(s.dtype, pd.CategoricalDtype):
out = get_dummies(s, drop_first=drop_first)
if s.hasnans:
out = out.astype(float).where(s.notna(), axis=0)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment. First assign the columns, then do the drop.

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.68%. Comparing base (7fa390b) to head (3616bbe).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #858   +/-   ##
=======================================
  Coverage   99.68%   99.68%           
=======================================
  Files         199      199           
  Lines       37875    37900   +25     
  Branches     2436     2438    +2     
=======================================
+ Hits        37756    37781   +25     
  Misses         51       51           
  Partials       68       68           
Flag Coverage Δ
adder 99.67% <100.00%> (+<0.01%) ⬆️
subtractor 99.67% <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.

2 participants