Repository navigation
fix(preprocessor): encode an all-missing categorical batch as object NaN - #160
Merged
Merged
Conversation
Without categorical imputation, _CategoricalMissingNormalizer is the only step before the one-hot encoder and passed non-object input through unchanged. pandas and polars type an all-missing column as float, so one serving row with an unknown category reached an encoder fitted on string categories and failed with a TypeError from np.isnan. The normalizer now returns a batch in which every value is missing as an object array of NaN. The encoder matches it to its missing category (or ignores it as unknown) for string and numeric categories alike. No fitted state is added, so fingerprints are unchanged. Fixes #159
mkumar73
approved these changes
Nov 30, 2026
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.
Fixes #159.
With
categorical_method="one-hot"and no categorical imputation,transformcrashed with aTypeErrorwhen a categorical column held only missing values. pandas types such a column asfloat64, and a polars all-null column converts to float. The case is common in serving: one row whose category is unknown.Root cause
Without an imputer,
_CategoricalMissingNormalizer(pretab/preprocessing/missing.py) is the only step before theOneHotEncoder. It normalizedobjectinput and passed any other dtype through unchanged, so a float NaN batch reached an encoder fitted on string categories. scikit-learn's_check_unknownthen callednp.isnanon those object categories.Fix
_CategoricalMissingNormalizer.transformreturns a non-empty batch in which every value is missing as anobjectarray of NaN. scikit-learn's encoder matches object NaN to the missing category seen atfit, or ignores it as unknown (handle_unknown="ignore"). This works for string and numeric categories alike.No fitted state is added. Preprocessors fitted on
mainand this branch have identical fingerprints, and existing specs and pickles behave as before.Behaviour changes
objectcolumn. Before, it raised aTypeErrorfor string categories.add_missing_indicator=Trueuses the same normalizer and also accepts the batch.Documentation
_CategoricalMissingNormalizerdocstring: an all-missing batch is returned as object NaN, and why.docs/core_concepts/missing_values.mdalready describes this behaviour for missing categorical values under"propagate", so no user-facing page changes. README, homepage and tutorials are unaffected.Tests
In
tests/integration/test_pandas_missing.py:test_all_missing_categorical_batch_matches_object_nan(one-hotandint× four missing configurations): the float64 row gives the same output as the object row. The three unimputedone-hotcases fail onmainand pass here. Theintandseparate_statecases are guards that pass on both.test_all_missing_one_hot_batch_encodes_as_missing_or_unknown(string or integer categories in an object column, with or without a missing category at fit): thenancolumn is 1 when it exists, otherwise all zeros. Fails onmain, passes here.test_all_missing_polars_column_is_encoded_like_pandas: fails onmain, passes here.Reproduction
The script from #159 on this branch:
Checks
just test: 4689 passed, 59 skipped, 7 xfailed; coverage 95.71% (gate 90%).scripts/quickstart.pypasses.just docs: strict build succeeds.just check: all pre-commit and pre-push hooks pass (file hygiene, ruff format, ruff lint, prettier, pyright with 0 errors).pretab.preprocessing.missing; no import direction changes.Merge notes
main. Git merges this branch cleanly with fix(binning): reject edges the soft encoding cannot use #150, fix(splines): choose thin-plate landmarks from complete rows #152, fix(preprocessor): accept NumPy scalars for cat_cutoff #154, fix(preprocessor): re-infer object array columns at fit #156 and fix(config): inherit NumPy scalar settings as Python values #158.