From 1033e4d018ea8d786c5fd05d832945f40e77b451 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:16:11 +0200 Subject: [PATCH] fix(preprocessor): keep the internal ColumnTransformer on default output A global sklearn.set_config(transform_output="pandas") (or "polars") reached the steps of the internal ColumnTransformer. The sparse OneHotEncoder refuses pandas and polars output, so categorical_method="one-hot" and preset="expanded" failed to fit with "Pandas output does not support sparse data", and users could not work around it because the encoder's sparse_output is not exposed. The internal ColumnTransformer is now pinned to set_output(transform="default"), so its steps always produce plain arrays. The Preprocessor still wraps its stacked output in the requested container itself, so transform keeps returning a pandas or polars DataFrame under the global setting. Fixes #76 --- docs/core_concepts/outputs_and_inspection.md | 4 ++ pretab/preprocessor.py | 9 ++++- tests/integration/test_output_format.py | 41 ++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/docs/core_concepts/outputs_and_inspection.md b/docs/core_concepts/outputs_and_inspection.md index b8b4d80..d48cba8 100644 --- a/docs/core_concepts/outputs_and_inspection.md +++ b/docs/core_concepts/outputs_and_inspection.md @@ -119,6 +119,10 @@ PreTab honours the scikit-learn output API, so you can request pandas or polars pre.set_output(transform="pandas") # or "polars" ``` +A global `sklearn.set_config(transform_output="pandas")` (or `"polars"`) is honoured the same +way. It applies to the `Preprocessor`'s own output only: the internal per-column steps always +produce arrays, so every method, including the sparse one-hot encoding, works under it. + ```{note} Polars output requires the optional `polars` extra (`pip install "pretab[polars]"`) and is loaded lazily: if polars is not installed, requesting it raises a clear diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index a8193b8..18eba89 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -580,6 +580,11 @@ def fit(self, X, y=None, embeddings=None): categorical_features, sparse_threshold=sparse_threshold, ) + # The Preprocessor wraps its own output (set_output / output_format), so the + # internal steps always produce plain arrays. Otherwise a global + # ``sklearn.set_config(transform_output="pandas")`` reaches them and the + # sparse OneHotEncoder refuses pandas output. + self.column_transformer_.set_output(transform="default") self.column_transformer_.fit(X, y) self.n_features_in_ = X.shape[1] @@ -631,7 +636,9 @@ def transform(self, X, embeddings=None, return_array=None): "matrix"``); a dict of per-feature blocks when ``output_structure="blocks"`` (or ``return_array=False``); a SciPy CSR matrix (or CSR blocks) when ``output_format`` resolves to ``"sparse"``; or a pandas / polars DataFrame - when configured via :meth:`set_output`. + when configured via :meth:`set_output` or a global + ``sklearn.set_config(transform_output=...)``. That setting applies to this + output only: the internal per-column steps always produce arrays. """ check_is_fitted(self) diff --git a/tests/integration/test_output_format.py b/tests/integration/test_output_format.py index abe2613..9faadbf 100644 --- a/tests/integration/test_output_format.py +++ b/tests/integration/test_output_format.py @@ -241,3 +241,44 @@ def test_invalid_output_format_raises(frame, y): with pytest.raises(InvalidParamError): _bspline(output_format="nope").fit(frame, y) + + +# --- global scikit-learn output config ---------------------------------------------- + + +@pytest.fixture +def mixed_frame(): + rng = np.random.default_rng(0) + a = rng.normal(size=40) + a[:4] = np.nan + return pd.DataFrame({"a": a, "color": rng.choice(["red", "green", "blue"], 40)}), rng.normal(size=40) + + +@pytest.mark.parametrize("options", [{"categorical_method": "one-hot"}, {"preset": "expanded"}]) +def test_one_hot_works_under_a_global_pandas_output_config(mixed_frame, options): + """The global transform_output reached the inner sparse OneHotEncoder, which refused it.""" + import sklearn + + X, y = mixed_frame + expected = np.asarray(Preprocessor(**options).fit(X, y).transform(X)) + with sklearn.config_context(transform_output="pandas"): + pre = Preprocessor(**options).fit(X, y) + out = pre.transform(X) + assert isinstance(out, pd.DataFrame) + assert list(out.columns) == list(pre.get_feature_names_out()) + assert set(map(str, out.dtypes)) == {"float64"} + np.testing.assert_allclose(out.to_numpy(), expected) + + +def test_global_polars_output_config_is_honoured(mixed_frame): + import sklearn + + pl = pytest.importorskip("polars") + X, y = mixed_frame + expected = np.asarray(Preprocessor(categorical_method="one-hot").fit(X, y).transform(X)) + with sklearn.config_context(transform_output="polars"): + pre = Preprocessor(categorical_method="one-hot").fit(X, y) + out = pre.transform(X) + assert isinstance(out, pl.DataFrame) + assert out.columns == list(pre.get_feature_names_out()) # type: ignore + np.testing.assert_allclose(np.asarray(out), expected)