From 13a8eb95b60dfcbad2f7d326540d8ab7c5e1e1a2 Mon Sep 17 00:00:00 2001 From: xillig <56161060+xillig@users.noreply.github.com> Date: Wed, 11 Nov 2026 05:28:11 +0100 Subject: [PATCH] fix(preprocessor): raise a typed error for input without rows at fit Preprocessor.fit on a frame, array or dict with columns but no rows failed with a bare ZeroDivisionError: column-type detection divides the number of unique values by the row count for the cat_cutoff ratio. detect_column_types now raises EmptyDataError (a PretabDataError and a ValueError, scikit-learn's convention for empty training data) that names the estimator and the input shape. Input without columns keeps fitting to an empty output, as before. --- docs/representations/choosing_a_method.md | 3 +++ pretab/compose/feature_detection.py | 13 ++++++++++- pretab/preprocessor.py | 3 +++ tests/compose/test_feature_detection.py | 17 +++++++++++++- tests/regression/test_edge_cases.py | 28 ++++++++++++++++++++++- 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/docs/representations/choosing_a_method.md b/docs/representations/choosing_a_method.md index 7524f36..a2c38ab 100644 --- a/docs/representations/choosing_a_method.md +++ b/docs/representations/choosing_a_method.md @@ -116,6 +116,9 @@ PreTab is explicit about degenerate inputs rather than failing silently. `"none"` keep its block as missing values, and every other method raises the same typed error. `policy={"constant": "error"}` rejects an empty numerical column like any other constant one. +- **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. ## Non-goals diff --git a/pretab/compose/feature_detection.py b/pretab/compose/feature_detection.py index 07db8f7..82ca19a 100644 --- a/pretab/compose/feature_detection.py +++ b/pretab/compose/feature_detection.py @@ -11,7 +11,7 @@ import pandas as pd from ..core.validation import is_polars_frame, polars_to_pandas -from ..exceptions import PretabDataError, invalid_param_error +from ..exceptions import EmptyDataError, PretabDataError, invalid_param_error __all__ = ["bool_columns_as_object", "detect_column_types", "has_column_labels", "to_dataframe", "with_string_labels"] @@ -137,8 +137,19 @@ def detect_column_types(X, *, cat_cutoff, treat_all_integers_as_numerical, estim Column labels detected as numerical. categorical_features : list Column labels detected as categorical. + + Raises + ------ + EmptyDataError + If ``X`` has columns but no rows: a column's kind is decided from its + values, so there is nothing to detect (or to fit) without a sample. """ X = to_dataframe(X) + if X.shape[0] == 0 and X.shape[1] > 0: + raise EmptyDataError( + f"{estimator_name} needs at least one sample, but X has 0 rows (shape {X.shape}).\n" + "Fix: pass data with at least one row." + ) categorical_features = [] numerical_features = [] diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index aaba9df..b35bff0 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -558,6 +558,9 @@ def fit(self, X, y=None, embeddings=None): Also if ``X`` has a duplicate column label, or two column labels share a string 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``). Warns ----- diff --git a/tests/compose/test_feature_detection.py b/tests/compose/test_feature_detection.py index 7018732..2cc3a37 100644 --- a/tests/compose/test_feature_detection.py +++ b/tests/compose/test_feature_detection.py @@ -10,7 +10,7 @@ to_dataframe, with_string_labels, ) -from pretab.exceptions import InvalidParamError, PretabDataError +from pretab.exceptions import EmptyDataError, InvalidParamError, PretabDataError def test_to_dataframe_wraps_ndarray_with_feature_names(): @@ -195,6 +195,21 @@ def test_invalid_cat_cutoff_type_raises(): detect_column_types(df, cat_cutoff="bad", treat_all_integers_as_numerical=False) +@pytest.mark.parametrize("cat_cutoff", [0.03, 5]) +def test_detect_column_types_rejects_input_without_rows(cat_cutoff): + """The unique-ratio cutoff divides by the row count; 0 rows raised ZeroDivisionError.""" + df = pd.DataFrame({"x": pd.Series([], dtype=float), "c": pd.Series([], dtype=object)}) + with pytest.raises(EmptyDataError, match=r"MyEstimator needs at least one sample.*0 rows \(shape \(0, 2\)\)"): + detect_column_types( + df, cat_cutoff=cat_cutoff, treat_all_integers_as_numerical=False, estimator_name="MyEstimator" + ) + + +def test_detect_column_types_keeps_accepting_input_without_columns(): + df = pd.DataFrame(index=range(3)) + assert detect_column_types(df, cat_cutoff=0.03, treat_all_integers_as_numerical=False) == ([], []) + + def test_with_string_labels_relabels_without_touching_the_input(): frame = pd.DataFrame({1: [1.0], "b": [2.0]}) relabelled = with_string_labels(frame) diff --git a/tests/regression/test_edge_cases.py b/tests/regression/test_edge_cases.py index 5733551..de44c0f 100644 --- a/tests/regression/test_edge_cases.py +++ b/tests/regression/test_edge_cases.py @@ -13,7 +13,7 @@ import pytest from pretab import Preprocessor -from pretab.exceptions import PretabDataError +from pretab.exceptions import EmptyDataError, PretabDataError def _finite(array) -> bool: @@ -110,3 +110,29 @@ def test_unseen_categories_do_not_crash(): # handle_unknown="ignore" encodes unseen categories as an all-zero row. assert out[2].sum() == 0.0 assert out[4].sum() == 0.0 + + +# --- input without rows -------------------------------------------------------- + + +@pytest.mark.parametrize( + "X", + [ + pd.DataFrame({"x": pd.Series([], dtype=float), "c": pd.Series([], dtype=object)}), + np.empty((0, 2)), + {"x": [], "c": []}, + ], + ids=["dataframe", "ndarray", "dict"], +) +@pytest.mark.parametrize("numerical_method", ["ple", "minmax"]) +def test_fit_on_input_without_rows_raises_typed_error(X, numerical_method): + """A 0-row fit used to fail with ZeroDivisionError during column-type detection.""" + pre = Preprocessor(numerical_method=numerical_method) + with pytest.raises(EmptyDataError, match=r"Preprocessor needs at least one sample, but X has 0 rows"): + pre.fit(X, []) + + +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)}), [])