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
3 changes: 3 additions & 0 deletions docs/representations/choosing_a_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion pretab/compose/feature_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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 = []
Expand Down
3 changes: 3 additions & 0 deletions pretab/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
17 changes: 16 additions & 1 deletion tests/compose/test_feature_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 27 additions & 1 deletion tests/regression/test_edge_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)}), [])