Skip to content
Merged
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
4 changes: 4 additions & 0 deletions docs/core_concepts/outputs_and_inspection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions pretab/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,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(self._column_transformer_input(X), y)
self.n_features_in_ = X.shape[1]
# scikit-learn convention: feature names are recorded only for named input.
Expand Down Expand Up @@ -677,8 +682,10 @@ 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` (a pandas DataFrame keeps the index
of ``X``).
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.
A pandas DataFrame keeps the index of ``X``.

Raises
------
Expand Down
56 changes: 56 additions & 0 deletions tests/integration/test_output_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,59 @@ 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("fit_before_config", [False, True])
@pytest.mark.parametrize(
"options",
[
{"categorical_method": "one-hot"},
{"preset": "expanded"},
{"feature_preprocessing": {"color": "one-hot"}},
],
)
def test_one_hot_works_under_a_global_pandas_output_config(mixed_frame, options, fit_before_config):
"""The global transform_output reached the inner sparse OneHotEncoder, which refused it."""
import sklearn

X, y = mixed_frame
X = X.set_axis(pd.Index(np.arange(len(X))[::-1] + 100, name="row_id"))
pre = Preprocessor(**options).fit(X, y)
expected = np.asarray(pre.transform(X))
with sklearn.config_context(transform_output="pandas"):
if not fit_before_config:
pre = Preprocessor(**options).fit(X, y)
out = pre.transform(X)
assert isinstance(out, pd.DataFrame)
pd.testing.assert_index_equal(out.index, X.index)
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)


@pytest.mark.parametrize("fit_before_config", [False, True])
def test_global_polars_output_config_is_honoured(mixed_frame, fit_before_config):
import sklearn

pl = pytest.importorskip("polars")
X, y = mixed_frame
pre = Preprocessor(categorical_method="one-hot").fit(X, y)
expected = np.asarray(pre.transform(X))
with sklearn.config_context(transform_output="polars"):
if not fit_before_config:
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)
Loading