Skip to content

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

Merged
bashtage merged 2 commits into
bashtage:mainfrom
jackiectl2:fix-categorical-missing-data
Nov 6, 2026
Merged

bashtage merged 2 commits into
bashtage:mainfrom
jackiectl2:fix-categorical-missing-data

Conversation

@jackiectl2

Copy link
Copy Markdown
Contributor

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.

@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 Outdated
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 46645aa. The dummy columns are named first; when the category has missing observations, columns with no observed 1s are then dropped before marking missing rows as NaN. I replaced s.hasnans with s.isna().any() for pandas compatibility. The new tests cover unused categories with and without drop_first; 216 IV/panel data tests, Ruff, and Black pass.

Comment thread linearmodels/panel/data.py Outdated
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied the same order in the panel conversion path in 46645aa: name the columns, drop unused dummies, and then mark missing rows. The panel regression cases pass.

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.68%. Comparing base (7fa390b) to head (46645aa).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
linearmodels/iv/data.py 80.00% 0 Missing and 1 partial ⚠️
linearmodels/panel/data.py 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #858      +/-   ##
==========================================
- Coverage   99.68%   99.68%   -0.01%     
==========================================
  Files         199      199              
  Lines       37875    37919      +44     
  Branches     2436     2440       +4     
==========================================
+ Hits        37756    37798      +42     
  Misses         51       51              
- Partials       68       70       +2     
Flag Coverage Δ
adder 99.67% <95.45%> (-0.01%) ⬇️
subtractor 99.67% <95.45%> (-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.

Signed-off-by: Tianlang (Jackie) Chen <[email protected]>
@bashtage
bashtage merged commit ec8b833 into bashtage:main Nov 6, 2026
14 of 15 checks passed
bashtage added a commit that referenced this pull request Nov 6, 2026
The follow-ups to #858, #859 and #862 (#867, #866, #865) and the Windows ARM64 job of the CI (#868).
s3alfisc pushed a commit to s3alfisc/linearmodels that referenced this pull request Nov 22, 2026
Follow-up to bashtage#858, which makes a missing value of a categorical column a
missing value of its dummy variables, so that the observation is dropped
instead of being kept as a row of zeros.

- IV2SLS and PooledOLS fit with a categorical column that has missing values
  drop those observations with a MissingValueWarning, and their estimates equal
  those of a least-squares fit of the same model without the observations,
  with dummies from pandas.get_dummies.
- A categorical column whose values are all missing keeps its dummy columns,
  and every observation is missing. This is the branch that codecov reported
  as uncovered in IVData and PanelData.
- The missing values of several categorical columns of IVData are combined.

The five new tests fail before bashtage#858. The convert_dummies parameter of IVData
and PanelData now says how missing categorical values are converted, with a
versionchanged note for 7.2.
s3alfisc pushed a commit to s3alfisc/linearmodels that referenced this pull request Nov 22, 2026
- Changes that alter results: missing categorical values are missing values
  of their dummy variables, so the observation is dropped (bashtage#858); the IV model
  F statistic leaves out an intercept that dummies imply (bashtage#859, which does
  not fix the other model families of bashtage#780).
- Bug fixes: the IV estimators do not store weighted copies of the data
  when all of the weights are 1 (bashtage#862, about a third less memory; the rest of
  bashtage#607, keeping float32, is not done).
- Compatibility: Python 3.10 and NumPy 1.x are still supported, although the
  7.1 notes said that 7.1 would be the last release for them. linearmodels 8.0
  will raise the minimum versions, as the README now says too.
- Contributors: jackiectl2, CodingSelim and DresdenGman.
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