Skip to content

Commit a30c65b

Browse files
committed
fix(sklearn_compat): cast pandas category columns to object in ensure_dataframe
1 parent 0b8b825 commit a30c65b

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

deeptab/core/sklearn_compat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def ensure_dataframe(X: Any, context: str = "fit") -> pd.DataFrame:
2424
- Empty DataFrames raise :exc:`~deeptab.core.exceptions.EmptyDataError`.
2525
- ``bool`` columns are silently cast to ``int8``; they represent valid binary
2626
features but sklearn's ``SimpleImputer`` rejects the ``bool`` dtype.
27+
- ``category`` columns are silently cast to ``object`` so they are detected and
28+
preprocessed as categorical features (the underlying categories are kept).
2729
- Any remaining non-numeric, non-object column dtype raises
2830
:exc:`~deeptab.core.exceptions.ColumnDtypeError` naming each offending column.
2931
- Columns where every value is NaN issue a
@@ -56,6 +58,15 @@ def ensure_dataframe(X: Any, context: str = "fit") -> pd.DataFrame:
5658
df = df.copy()
5759
df[bool_cols] = df[bool_cols].astype("int8")
5860

61+
# category → object: treat pandas categoricals as categorical features.
62+
# The dtype detector downstream keys off object/string dtype, so cast here
63+
# while preserving the underlying category values.
64+
cat_cols = [c for c, dt in df.dtypes.items() if isinstance(dt, pd.CategoricalDtype)]
65+
if cat_cols:
66+
if not bool_cols:
67+
df = df.copy()
68+
df[cat_cols] = df[cat_cols].astype(object)
69+
5970
# Catch any other dtype that is neither numeric nor object/string
6071
bad_cols = [
6172
(c, dt)

0 commit comments

Comments
 (0)