Skip to content

fix(preprocessor): encode an all-missing categorical batch as object NaN - #160

Merged
mkumar73 merged 1 commit into
mainfrom
fix/159-all-missing-categorical-batch
Nov 30, 2026
Merged

mkumar73 merged 1 commit into
mainfrom
fix/159-all-missing-categorical-batch

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #159.

With categorical_method="one-hot" and no categorical imputation, transform crashed with a TypeError when a categorical column held only missing values. pandas types such a column as float64, 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 the OneHotEncoder. It normalized object input and passed any other dtype through unchanged, so a float NaN batch reached an encoder fitted on string categories. scikit-learn's _check_unknown then called np.isnan on those object categories.

Fix

_CategoricalMissingNormalizer.transform returns a non-empty batch in which every value is missing as an object array of NaN. scikit-learn's encoder matches object NaN to the missing category seen at fit, or ignores it as unknown (handle_unknown="ignore"). This works for string and numeric categories alike.

No fitted state is added. Preprocessors fitted on main and this branch have identical fingerprints, and existing specs and pickles behave as before.

Behaviour changes

  • An all-missing categorical batch encodes exactly like the same missing values in an object column. Before, it raised a TypeError for string categories.
  • Batches with at least one observed value, numeric categorical columns with observed values, and sparse input are unchanged.
  • The indicator branch of add_missing_indicator=True uses the same normalizer and also accepts the batch.

Documentation

  • _CategoricalMissingNormalizer docstring: an all-missing batch is returned as object NaN, and why.
  • docs/core_concepts/missing_values.md already 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-hot and int × four missing configurations): the float64 row gives the same output as the object row. The three unimputed one-hot cases fail on main and pass here. The int and separate_state cases 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): the nan column is 1 when it exists, otherwise all zeros. Fails on main, passes here.
  • test_all_missing_polars_column_is_encoded_like_pandas: fails on main, passes here.

Reproduction

The script from #159 on this branch:

row dtypes: {'age': 'float64', 'city': 'float64'}
{'missing_policy': 'propagate'}
  object NaN -> [[0.4871794871794871, 0.0, 0.0, 0.0, 1.0]]
  float NaN  -> [[0.4871794871794871, 0.0, 0.0, 0.0, 1.0]]
  polars null -> [[0.4871794871794871, 0.0, 0.0, 0.0, 1.0]]
{'categorical_imputation': None}
  object NaN -> [[0.4871794871794871, 0.0, 0.0, 0.0, 1.0]]
  float NaN  -> [[0.4871794871794871, 0.0, 0.0, 0.0, 1.0]]
  polars null -> [[0.4871794871794871, 0.0, 0.0, 0.0, 1.0]]

Checks

  • just test: 4689 passed, 59 skipped, 7 xfailed; coverage 95.71% (gate 90%).
  • Minimum dependencies (CI job: Python 3.11, numpy 1.24.4, pandas 2.0.3, scipy 1.10.1, scikit-learn 1.6.0): 4505 passed, 174 skipped, 7 xfailed; scripts/quickstart.py passes.
  • 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).
  • The change stays inside pretab.preprocessing.missing; no import direction changes.

Merge notes

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
mkumar73 merged commit 204e97b into main Nov 30, 2026
19 checks passed
@mkumar73 mkumar73 self-assigned this Nov 30, 2026
@mkumar73
mkumar73 deleted the fix/159-all-missing-categorical-batch branch November 30, 2026 07:27
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.

One-hot without categorical imputation crashes on an all-missing categorical column at transform (e.g. one serving row)

2 participants