Skip to content

FIX FeatureUnion for polars output with intermediate duplicated column names - #32106

Merged
lorentzenchr merged 14 commits into
scikit-learn:mainfrom
jeremiedbb:fix-feature-union-polars-output
May 21, 2026
Merged

lorentzenchr merged 14 commits into
scikit-learn:mainfrom
jeremiedbb:fix-feature-union-polars-output

Conversation

@jeremiedbb

@jeremiedbb jeremiedbb commented Sep 4, 2025 •

Copy link
Copy Markdown
Member

Fixes #32104

@glemaitre or @thomasjpfan maybe ?

@github-actions

github-actions Bot commented Sep 4, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: d66e21e. Link to the linter CI: here

Comment thread sklearn/pipeline.py Resolved
Co-authored-by: Jérôme Dockès <[email protected]>
@jeremiedbb

jeremiedbb commented Sep 5, 2025 •

Copy link
Copy Markdown
Member Author

Hum, actually this fix is not enough. When verbose_feature_names_out=False, it doesn't use get_feature_names_out but keep the current column names instead (i.e. with the meaningless intermediate names that I added here).

So a proper fix would be to do something like in the ColumnTransformer, that is if verbose_feature_names_out=False, obtain each transformer out feature names and use those before stacking, and raise an informative error in case of duplicate column names.

EDIT: Actually that was a bug that I fixed in this PR, see #32106 (comment), so we can keep the simple fix

@jeremiedbb
jeremiedbb marked this pull request as draft September 5, 2025 13:07
Comment on lines +30 to +34
except AttributeError as e:
if "does not provide get_feature_names_out" in str(e):
return None
else:
raise

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note for reviewers: this was bypassing the check for duplicates in _add_prefix_for_feature_names_out (itself called in get_feature_names_out), explaining why the error did not happen with pandas even when verbose_feature_names_out was set to False.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, with the changes from this PR, I don't think that we ever need the try except at all. The coverage shows that the except part is not covered by our tests and I confirmed that by removing it locally and run the full test suite without error.

That said, I'm okay to keep it to be extra safe in case it covers some situations that are not covered by the test suite. I was not able to find one but I'm curious if someone else has an idea :)

@jeremiedbb

jeremiedbb commented Sep 16, 2025 •

Copy link
Copy Markdown
Member Author

I took the opportunity to clean-up the whole logic in the ColumnTransformer as well. There's no need to compute the feature names and do the duplicate columns here. It should be done by the set_output wrapper.
EDIT: I extracted the column transformer part in #32205

@jeremiedbb jeremiedbb moved this to In progress in Labs Feb 1, 2026
@jeremiedbb jeremiedbb added this to Labs Feb 1, 2026
@jeremiedbb

Copy link
Copy Markdown
Member Author

Fixed by #32853

@jeremiedbb jeremiedbb closed this Feb 2, 2026
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Feb 2, 2026
@jeremiedbb jeremiedbb reopened this Feb 15, 2026
@jeremiedbb

Copy link
Copy Markdown
Member Author

I'm reopening this PR because #32853 did not completely fixed the issue (and introduced a regression at the same time) because it did not take transformers that don't implement get_featur_names_out into account.

@jeremiedbb

Copy link
Copy Markdown
Member Author

I reverted the now unnecessary parts of #32853 here but I'm not sure what to do with the changelog entry

@jeremiedbb jeremiedbb moved this from Done to In progress in Labs Feb 15, 2026
@jeremiedbb jeremiedbb added Waiting for Second Reviewer First reviewer is done, need a second one! and removed Waiting for Second Reviewer First reviewer is done, need a second one! labels Feb 15, 2026
@betatim

betatim commented Feb 16, 2026

Copy link
Copy Markdown
Member

Can we amend the changelog entry to refer to this PR and add a changelog entry for this PR mentioning that it is a followup?

@jeremiedbb

Copy link
Copy Markdown
Member Author

Can we amend the changelog entry to refer to this PR and add a changelog entry for this PR mentioning that it is a followup?

So actually I added the author of the other PR in this PR's changelog fragment and deleted the other fragment because I think it would be misleading to have a changelog entry pointing to a PR that is not the appropriate fix and for which the code is not in the codebase in the end.

@jeremiedbb jeremiedbb added this to the 1.9 milestone Mar 9, 2026
@ogrisel

ogrisel commented May 15, 2026

Copy link
Copy Markdown
Member

I pushed a merge commit with conflict resolution (deletion) of the old changelog entry.

Comment thread sklearn/pipeline.py Outdated
# The names are not important here as final column names will be
# generated by the set_output wrapper using `get_feature_names_out`.
Xs = [
adapter.rename_columns(X, [f"{i}_{j}" for j in range(X.shape[1])])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this mutates X in place, which means we would end up mutating the columns of the input's columns. Here is a reproducer:

from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import StandardScaler
import pandas as pd

df = pd.DataFrame({"a": [1.0, 2, 3, 4], "b": [1.0, 2, 3, 4]})
before = list(df.columns)

fu = FeatureUnion([("t1", "passthrough"), ("t2", StandardScaler())])
fu.set_output(transform="pandas")
out = fu.fit_transform(df)

after = list(df.columns)
assert before == after, f"{before=}, {after=}"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oo, that's bad ! Thanks
I pushed a fix to copy the dataframes before. It should not have an impact on memory since for both pandas and polars, copy doesn't copy the underlying data.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeremiedbb jeremiedbb May 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep narwhals out for this PR because it's just a small bug fix, while the whole set_output stuff could benefit from it.

I think that there's a lot to do with narwhals and set_output, like the whole adapter thing looks like a mini narwhals :), but in a different refactoring PR.

Comment thread sklearn/pipeline.py Resolved
@lorentzenchr
lorentzenchr enabled auto-merge (squash) May 21, 2026 07:53
@lorentzenchr
lorentzenchr merged commit 5b3f54b into scikit-learn:main May 21, 2026
37 of 38 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs May 21, 2026
@jeremiedbb jeremiedbb added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label May 21, 2026
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
jeremiedbb added a commit that referenced this pull request Jun 2, 2026
…n names (#32106)

Co-authored-by: Jérôme Dockès <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
@jeremiedbb
jeremiedbb deleted the fix-feature-union-polars-output branch September 3, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:pipeline No Changelog Needed To backport PR merged in master that need a backport to a release branch defined based on the milestone.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FeatureUnion with polars output can error due to duplicate column names

6 participants