Repository navigation
FIX FeatureUnion for polars output with intermediate duplicated column names - #32106
Conversation
Co-authored-by: Jérôme Dockès <[email protected]>
|
Hum, actually this fix is not enough. When So a proper fix would be to do something like in the EDIT: Actually that was a bug that I fixed in this PR, see #32106 (comment), so we can keep the simple fix |
… into fix-feature-union-polars-output
| except AttributeError as e: | ||
| if "does not provide get_feature_names_out" in str(e): | ||
| return None | ||
| else: | ||
| raise |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
|
|
|
Fixed by #32853 |
|
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 |
|
I reverted the now unnecessary parts of #32853 here but I'm not sure what to do with the changelog entry |
|
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. |
|
I pushed a merge commit with conflict resolution (deletion) of the old changelog entry. |
| # 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])]) |
There was a problem hiding this comment.
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=}"There was a problem hiding this comment.
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.
There was a problem hiding this comment.
We could also use narwhals and avoid calling copy, see the example of https://narwhals-dev.github.io/narwhals/api-reference/dataframe/#narwhals.dataframe.DataFrame.rename.
There was a problem hiding this comment.
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.
…n names (#32106) Co-authored-by: Jérôme Dockès <[email protected]> Co-authored-by: Olivier Grisel <[email protected]>
Fixes #32104
@glemaitre or @thomasjpfan maybe ?