Skip to content

MNT Simplify hstack logic in ColumnTransformer - #32205

Merged
jeremiedbb merged 16 commits into
scikit-learn:mainfrom
jeremiedbb:mnt-column-transformer-names-out
Apr 9, 2026
Merged

jeremiedbb merged 16 commits into
scikit-learn:mainfrom
jeremiedbb:mnt-column-transformer-names-out

Conversation

@jeremiedbb

Copy link
Copy Markdown
Member

noticed while working on #32106. I extracted here the part that is only about the ColumnTransformer.

In #28262, a lot of complexity was added to the _hstack method to raise an error when the concatenated dataframe would have duplicate column names. However this logic is already implemented in get_feature_names_out.

We can simplify it a lot by storing the feature names and let get_feature_names_out handle the renaming and errors as it was designed to. At this stage, to avoid duplicate feature names, we can just use temporary unique names since they will be renamed anyway at the end by the set_output wrapper.

I also merged the 2 existing tests that test this part since their only diff was verbose_feature_names_out=True|False, and extended them to check a wider range of transformers, in particular with transformers that don't define get_feature_names_out.

@github-actions

github-actions Bot commented Sep 17, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

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

Generated for commit: 8d7e21a. Link to the linter CI: here

Comment on lines 27 to 35
if callable(columns):
try:
return columns()
except Exception:
return None
except AttributeError as e:
if "does not provide get_feature_names_out" in str(e):
return None
else:
raise
return columns

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.

The coverage failure is due to the except part that is not now covered. It's still necessary though because it's useful for FreatureUnion but not tested. Tests for FeatureUnion are added in #32106

@adrinjalali adrinjalali left a comment

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.

Can you please explain the changes in the tests? It feels odd to simplify the code, and at the same time having to change the tests so much 😁

Comment thread sklearn/compose/_column_transformer.py Outdated
Comment on lines +624 to +637
if hasattr(trans, "get_feature_names_out"):
column_indices = self._transformer_to_input_indices[name]
names = input_features[column_indices]
feature_names_out = trans.get_feature_names_out(names)
elif hasattr(self, "_transformers_feature_names_out"):
# Fallback to feature names returned by transformers that output
# dataframes but don't implement get_feature_names_out.
feature_names_out = self._transformers_feature_names_out[i]
else:
raise AttributeError(
f"Transformer {name} (type {type(trans).__name__}) does "
"not provide get_feature_names_out."
)

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.

Looks like it would have been cleaner to keep it in the helper function?

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.

Done

Comment thread sklearn/compose/_column_transformer.py Outdated
skip_empty_columns=True,
# Store feature names out of transformers in case they don't implement
# get_feature_names_out
self._transformers_feature_names_out = [X.columns for X in Xs]

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.

use _get_feature_names from validation instead?

list(zip(transformer_names, feature_names_outs))
)
else:
# check for duplicated columns and raise if any

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.

what's happening now with duplicate names then?

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.

It's detected by the set_output wrapper which raises an error. It was the expected behavior from the start but bypassed due to a bug. The previous bug fix in #28262 was overly complicated and introduced a custom check for this instead of finding a way to not bypass the set_output wrapper own check.

Comment on lines +2591 to +2592
transformer.set_params(verbose_feature_names_out=False)
with pytest.raises(ValueError, match=r"Output feature names:.*are not unique"):

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.

The test that I removed was almost the same as this one except that it checked the verbose_feature_names_out=False part. That's why I merged both tests into a single one.

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.

Note that the error message is not the same because it's now handled by the set_output machinery as it was expected from the start.

Comment on lines +2533 to +2547
# DfOutTransformer that does not define get_feature_names_out
class DfOutTransformer(BaseEstimator):
def __init__(self, offset=1.0):
self.offset = offset

def fit(self, X, y=None):
return self

def transform(self, X, y=None):
return X - self.offset

def set_output(self, transform=None):
# This transformer will always output a DataFrame regardless of the
# configuration.
return self

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 added that to have a more robust test for 3rd party estimators

@jeremiedbb

Copy link
Copy Markdown
Member Author

Can you please explain the changes in the tests? It feels odd to simplify the code, and at the same time having to change the tests so much 😁

Indeed :) I updated it to have a much smaller diff. The only 2 changes are now

  • added a custom transformer to check for exotic transformers
  • removed the 2nd test and merged it with the first one because they are almost the same. The error message is different because now it's the error message from the set_output wrapper instead of the custom message from the previous bug fix PR

@adrinjalali adrinjalali left a comment •

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.

LGTM

@jeremiedbb
jeremiedbb merged commit 506ab6e into scikit-learn:main Apr 9, 2026
32 of 33 checks passed
@jeremiedbb

Copy link
Copy Markdown
Member Author

Merging this maintenance PR with 1 approval.

@jeremiedbb
jeremiedbb deleted the mnt-column-transformer-names-out branch September 3, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants