From 08db61b1f6733c6a6138fd3d7bd8d0df7d0158a3 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Sun, 29 Nov 2026 13:59:06 +0100 Subject: [PATCH] fix(preprocessor): reject input without columns at fit 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 --- docs/representations/choosing_a_method.md | 2 ++ pretab/preprocessor.py | 12 ++++++++++-- tests/regression/test_edge_cases.py | 24 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/docs/representations/choosing_a_method.md b/docs/representations/choosing_a_method.md index 8f6368c..5014ba2 100644 --- a/docs/representations/choosing_a_method.md +++ b/docs/representations/choosing_a_method.md @@ -127,6 +127,8 @@ PreTab is explicit about degenerate inputs rather than failing silently. - **Input without rows at fit**: `Preprocessor.fit` raises `EmptyDataError` (a `PretabDataError`, and so a `ValueError`): a column's kind is detected from its values, so nothing can be fitted without a sample. +- **Input without columns at fit**: `Preprocessor.fit` raises the same `EmptyDataError`, for + example when every column was dropped upstream: there is no feature to represent. ## Non-goals diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index 59879a7..7510e65 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -59,6 +59,7 @@ from .exceptions import ( ConfigWarning, DataWarning, + EmptyDataError, FrozenRepresentationError, IncompatibleParamsError, OutputBudgetError, @@ -596,8 +597,8 @@ def fit(self, X, y=None, embeddings=None): form (e.g. ``1`` and ``"1"``): every column is routed by the string form of its label, so such columns could not be told apart. EmptyDataError - If ``X`` has columns but no rows (a subclass of ``PretabDataError`` and - ``ValueError``). + If ``X`` has columns but no rows, or no columns at all (a subclass of + ``PretabDataError`` and ``ValueError``). Warns ----- @@ -632,6 +633,13 @@ def fit(self, X, y=None, embeddings=None): fitted_on_array = not has_column_labels(X) X = to_dataframe(X) + if X.shape[1] == 0: + # A ColumnTransformer without columns fits, but has no output layout to + # slice into blocks or lineage, so the "fitted" object fails later. + raise EmptyDataError( + f"{type(self).__name__} needs at least one feature, but X has 0 columns (shape {X.shape}).\n" + "Fix: pass data with at least one column." + ) self._validate_feature_overrides(X) diff --git a/tests/regression/test_edge_cases.py b/tests/regression/test_edge_cases.py index de44c0f..d09cfa3 100644 --- a/tests/regression/test_edge_cases.py +++ b/tests/regression/test_edge_cases.py @@ -136,3 +136,27 @@ def test_fit_on_input_without_rows_is_a_value_error(): """scikit-learn's convention for empty training data.""" with pytest.raises(ValueError, match="0 rows"): Preprocessor().fit_transform(pd.DataFrame({"x": pd.Series([], dtype=float)}), []) + + +# --- input without columns ----------------------------------------------------- + + +@pytest.mark.parametrize( + "X", + [pd.DataFrame(index=range(5)), np.empty((5, 0)), [[], [], [], [], []]], + ids=["dataframe", "ndarray", "rows"], +) +def test_fit_on_input_without_columns_raises_typed_error(X): + """A 0-column fit succeeded, and the 'fitted' preprocessor then failed in block + output and lineage because its ColumnTransformer had no output layout.""" + pre = Preprocessor() + with pytest.raises(EmptyDataError, match=r"Preprocessor needs at least one feature, but X has 0 columns"): + pre.fit(X) + assert pre.lifecycle_state_ == "UNFITTED" + assert not hasattr(pre, "column_transformer_") + + +def test_fit_on_input_without_columns_is_a_value_error(): + """scikit-learn's check_estimators_empty_data_messages expects a ValueError.""" + with pytest.raises(ValueError, match="0 columns"): + Preprocessor().fit_transform(np.empty((3, 0)), np.zeros(3))