Skip to content

fix(preprocessor): reject input without columns at fit - #166

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/165-input-without-columns
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/165-input-without-columns

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #165.

Preprocessor.fit accepted input without columns and reported FITTED. transform(return_array=False) and get_feature_lineage() then failed with AttributeError: 'ColumnTransformer' object has no attribute 'output_indices_'.

Root cause

Only input without rows was rejected, in detect_column_types. A ColumnTransformer without transformers fits but never sets output_indices_, which the block output and the lineage read.

Fix

Preprocessor.fit (pretab/preprocessor.py) raises EmptyDataError right after converting X to a frame when it has no columns. The message mirrors the zero-row one: Preprocessor needs at least one feature, but X has 0 columns (shape (5, 0)). The check runs before any fitted attribute is set, so the preprocessor stays UNFITTED.

I put the check in fit, not in detect_column_types, so this branch does not overlap the edits that #154 and #156 make in pretab/compose/feature_detection.py.

Behaviour changes

  • fit on input without columns raises EmptyDataError (a PretabDataError and ValueError) instead of producing an unusable fitted object.
  • Wrappers that fit a Preprocessor (CrossFittedTransformer, RepresentationSearchCV, scikit-learn pipelines) surface the same error.

Documentation

  • Preprocessor.fit docstring (Raises): EmptyDataError also covers input without columns.
  • docs/representations/choosing_a_method.md (edge cases): new "Input without columns at fit" entry next to the zero-row one.

Tests

In tests/regression/test_edge_cases.py:

  • test_fit_on_input_without_columns_raises_typed_error (DataFrame, ndarray, list of empty rows): typed error, UNFITTED state, no column_transformer_. Fails on main, passes here.
  • test_fit_on_input_without_columns_is_a_value_error: scikit-learn's empty-data convention. Fails on main, passes here.

Reproduction

The script from #165 on this branch:

fit -> EmptyDataError: Preprocessor needs at least one feature, but X has 0 columns (shape (5, 0)).
0 rows for comparison -> EmptyDataError: Preprocessor needs at least one sample, but X has 0 rows (shape (0, 1)).

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 facade imports EmptyDataError from pretab.exceptions; no import direction changes.

Merge notes

fit accepted a frame or array with zero columns and reported itself as
fitted. Its ColumnTransformer had no output layout (output_indices_), so
transform(return_array=False) and get_feature_lineage() then failed with
an AttributeError from scikit-learn internals.

fit now raises EmptyDataError for input without columns, parallel to the
existing check for input without rows, and the preprocessor stays
unfitted.

Fixes #165

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.

Preprocessor.fit accepts input without columns, and the fitted object then crashes in block output and lineage

1 participant