Skip to content

fix(preprocessor): re-infer object array columns at fit - #156

Merged
mkumar73 merged 2 commits into
mainfrom
fix/155-object-array-fit-types
Nov 30, 2026
Merged

mkumar73 merged 2 commits into
mainfrom
fix/155-object-array-fit-types

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #155.

Preprocessor.fit on an object ndarray detected every column as categorical, so a numeric column of df.to_numpy() (or of a SimpleImputer(strategy="most_frequent") output on mixed data) was one-hot or ordinal encoded with one category per distinct value. transform already re-inferred those columns, so fit and transform disagreed.

Root cause

to_dataframe (pretab/compose/feature_detection.py) wrapped an array as-is, keeping every column of an object array as object. transform called infer_objects() in _align_input, but fit, the feature-group validation in fit and _preflight_task_target did not.

Fix

  • to_dataframe calls infer_objects() when it wraps input without column labels (an array or a list of rows). Every entry point goes through it, so fit, the group validation, the task preflight and transform see the same dtypes.
  • _align_input (pretab/preprocessor.py) no longer repeats the call.
  • Labelled pandas frames, dicts and polars frames keep their dtypes, and numeric arrays are unaffected.

Behaviour changes

  • A numeric column of an object array is now detected as numerical at fit, with the integer cat_cutoff heuristic applied as for a DataFrame. Before, it was always categorical. A preprocessor fitted on an object array therefore gets a different (much smaller) layout and fingerprint than on main.
  • Specs written before this change still load and transform as fitted, because the fitted layout is stored in the spec.
  • A list of rows that mixes numbers and strings is still read by NumPy as a string array. That is unchanged and out of scope.

Documentation

  • to_dataframe docstring and the Preprocessor.fit X parameter: the numeric columns of an object array are re-inferred before the column types are detected.
  • docs/core_concepts/configuration.md (array input): object-array columns are re-inferred, with df.to_numpy() and SimpleImputer as examples.
  • The README statement ("infers numerical versus categorical columns either way") and the transform docstring now hold as written.

Tests

  • tests/compose/test_feature_detection.py::test_to_dataframe_reinfers_the_columns_of_an_object_array: fails on main, passes here. test_to_dataframe_keeps_the_dtypes_of_a_labelled_frame is a guard that passes on both.
  • tests/integration/test_preprocessor.py::test_object_array_fit_detects_the_column_types_of_the_frame: same types, widths and output as the DataFrame fit. Fails on main, passes here.
  • tests/integration/test_preprocessor.py::test_imputed_mixed_frame_keeps_its_numerical_columns: SimpleImputer → Preprocessor pipeline. Fails on main, passes here.
  • tests/core/test_cross_fitted.py::test_fit_transform_keeps_strings_left_unchanged: this test relied on the float column of its object array being misdetected as categorical and passed through by categorical_method=None. It now passes the float column through with numerical_method="none", scaling="none" and asserts the detected types. Its purpose, strings surviving cross-fitting unchanged in an object output, is unchanged.

Reproduction

The script from #155 on this branch:

DataFrame fit   : ['age'] ['city'] width 4
object array fit: ['feature_0'] ['feature_1'] width 4
   first names: ['num_feature_0', 'cat_feature_1_Lima', 'cat_feature_1_Oslo']
DataFrame-fitted transform(X) == transform(df): True
SimpleImputer -> Preprocessor: ['feature_0'] ['feature_1'] width 4

Checks

  • just test: 4680 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): 4497 passed, 173 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 in pretab.compose.feature_detection and the facade; no import direction changes.

Merge notes

to_dataframe wrapped an object ndarray (df.to_numpy() of mixed data, or
the output of SimpleImputer on a mixed frame) with every column as
object, so fit detected numeric columns as categorical and one-hot or
ordinal encoded each distinct number. transform already called
infer_objects() for arrays, so fit and transform disagreed.

to_dataframe now re-infers the columns of input without labels, which
covers fit, the feature-group validation and the task preflight as well
as transform; _align_input no longer repeats it. Labelled frames keep
their dtypes.

Fixes #155
@mkumar73
mkumar73 merged commit 06c272c into main Nov 30, 2026
19 checks passed
@mkumar73
mkumar73 deleted the fix/155-object-array-fit-types branch November 30, 2026 07:25
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.

Preprocessor.fit on an object ndarray treats numeric columns as categorical, unlike transform

2 participants