Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/representations/choosing_a_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions pretab/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from .exceptions import (
ConfigWarning,
DataWarning,
EmptyDataError,
FrozenRepresentationError,
IncompatibleParamsError,
OutputBudgetError,
Expand Down Expand Up @@ -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
-----
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions tests/regression/test_edge_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Loading