8000 FIX Fixes ColumnTransformer to support empty selection for pandas output by thomasjpfan · Pull Request #25570 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX Fixes ColumnTransformer to support empty selection for pandas output #25570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 9, 2023
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
< 8000 div class="container-md js-file-filter-blankslate" data-target="diff-file-filter.blankslate" hidden>

There are no files selected for viewing

7 changes: 7 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ Changelog
fail due to a permutation of the labels when running multiple inits.
:pr:`25563` by :user:`Jérémie du Boisberranger <jeremiedbb>`.

:mod:`sklearn.compose`
......................

- |Fix| Fixes a bug in :class:`compose.ColumnTransformer` which now supports
empty selection of columns when `set_output(transform="pandas")`.
:pr:`25570` by `Thomas Fan`_.

:mod:`sklearn.isotonic`
.......................

Expand Down
4 changes: 3 additions & 1 deletion sklearn/compose/_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,9 @@ def _hstack(self, Xs):
transformer_names = [
t[0] for t in self._iter(fitted=True, replace_strings=True)
]
feature_names_outs = [X.columns for X in Xs]
# Selection of columns might be empty.
# Hence feature names are filtered for non-emptiness.
feature_names_outs = [X.columns for X in Xs if X.shape[1] != 0]
names_out = self._add_prefix_for_feature_names_out(
list(zip(transformer_names, feature_names_outs))
)
Expand Down
29 changes: 29 additions & 0 deletions sklearn/compose/tests/test_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,3 +2129,32 @@ def test_transformers_with_pandas_out_but_not_feature_names_out(
ct.set_params(verbose_feature_names_out=False)
X_trans_df1 = ct.fit_transform(X_df)
assert_array_equal(X_trans_df1.columns, expected_non_verbose_names)


@pytest.mark.parametrize(
"empty_selection",
[[], np.array([False, False]), [False, False]],
ids=["list", "bool", "bool_int"],
)
def test_empty_selection_pandas_output(empty_selection):
"""Check that pandas output works when there is an empty selection.

Non-regression test for gh-25487
"""
pd = pytest.importorskip("pandas")

X = pd.DataFrame([[1.0, 2.2], [3.0, 1.0]], columns=["a", "b"])
ct = ColumnTransformer(
[
("categorical", "passthrough", empty_selection),
("numerical", StandardScaler(), ["a", "b"]),
],
verbose_feature_names_out=True,
)
ct.set_output(transform="pandas")
X_out = ct.fit_transform(X)
assert_array_equal(X_out.columns, ["numerical__a", "numerical__b"])

ct.set_params(verbose_feature_names_out=False)
X_out = ct.fit_transform(X)
assert_array_equal(X_out.columns, ["a", "b"])
0