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
13 changes: 13 additions & 0 deletions pretab/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .core.exceptions import (
IncompatibleParamsError,
PretabDataError,
invalid_param_error,
)
from .core.logging import configure_logging, get_logger
Expand Down Expand Up @@ -285,6 +286,18 @@ def _detect_column_types(self, X):
elif isinstance(X, np.ndarray):
X = pd.DataFrame(X, columns=[f"feature_{i}" for i in range(X.shape[1])])

# ``X[col]`` returns a DataFrame rather than a Series for a duplicated
# label, so the dtype inspection below fails with an opaque
# ``AttributeError``. The ColumnTransformer this builds also keys its
# transformers by column name, so duplicates could not be routed even if
# detection coped with them.
duplicated = X.columns[X.columns.duplicated()].unique().tolist()
if duplicated:
raise PretabDataError(
f"Duplicate column names are not supported: {duplicated}.\n"
"Fix: rename the columns so every name is unique."
)

for col in X.columns:
num_unique_values = X[col].nunique()
total_samples = len(X[col])
Expand Down
33 changes: 33 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,36 @@ def test_preprocessor_unknown_categorical_method():
with pytest.raises(InvalidParamError) as exc:
Preprocessor(categorical_method="bogus").fit(df, y)
assert isinstance(exc.value, ValueError)


# --------------------------------------------------------------------------- #
# Duplicate column names must fail with a clear, actionable error.
#
# ``X[col]`` returns a DataFrame rather than a Series for a duplicated label, so
# detection died on ``.dtype`` with an opaque AttributeError from pandas.
# --------------------------------------------------------------------------- #
def test_duplicate_column_names_raise_a_clear_error():
rng = np.random.default_rng(0)
frame = pd.DataFrame(np.column_stack([rng.normal(size=50)] * 2), columns=pd.Index(["a", "a"]))

with pytest.raises(PretabDataError, match=r"Duplicate column names are not supported: \['a'\]"):
Preprocessor(numerical_method="minmax").fit(frame, rng.normal(size=50))


def test_duplicate_column_names_lists_every_offender():
rng = np.random.default_rng(0)
frame = pd.DataFrame(rng.normal(size=(50, 4)), columns=pd.Index(["a", "a", "b", "b"]))

with pytest.raises(PretabDataError) as excinfo:
Preprocessor(numerical_method="minmax").fit(frame, rng.normal(size=50))

assert "'a'" in str(excinfo.value) and "'b'" in str(excinfo.value)


def test_unique_column_names_are_unaffected():
rng = np.random.default_rng(0)
frame = pd.DataFrame(rng.normal(size=(50, 2)), columns=pd.Index(["a", "b"]))

pre = Preprocessor(numerical_method="minmax").fit(frame, rng.normal(size=50))

assert sorted(pre.output_dims_) == ["a", "b"]
Loading