FeatureUnion with polars output can error due to duplicate column names

### Describe the bug

FeatureUnion concatenates outputs of its transformers _before_ the `set_output` wrapper renames columns based on `get_feature_names_out` (adding the transformer name prefix). This works with pandas but not polars which does not allow creating a dataframe with duplicate feature names

in addition to the reproducer below, `pytest sklearn/tests/test_pipeline.py::test_feature_union_set_output` fails if we replace "pandas" with "polars" in the test

### Steps/Code to Reproduce

```python
import polars as pl
import pandas as pd
from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import StandardScaler

df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [1, 2, 3, 4]})
fu = FeatureUnion([("std_1", StandardScaler()), ("std_2", StandardScaler())])
fu.set_output(transform="pandas")
fu.fit_transform(df) # OK

# result:
#
#    std_1__a  std_1__b  std_2__a  std_2__b
# 0 -1.341641 -1.341641 -1.341641 -1.341641
# 1 -0.447214 -0.447214 -0.447214 -0.447214
# 2  0.447214  0.447214  0.447214  0.447214
# 3  1.341641  1.341641  1.341641  1.341641

df = pl.from_pandas(df)
fu.set_output(transform="polars")
fu.fit_transform(df) # ERROR during hstack step as both transformers have output column names ['a', 'b']

# error:
# Traceback (most recent call last):
#     ...
# polars.exceptions.DuplicateError: column with name 'a' has more than one occurrence
```

### Expected Results

dataframe with column names `std_1__a  std_1__b  std_2__a  std_2__b`

### Actual Results
```
Traceback (most recent call last):
  File ".../feature_union.py", line 26, in <module>
    fu.fit_transform(df) # ERROR during hstack step as both transformers have output column names ['a', 'b']
    ~~~~~~~~~~~~~~~~^^^^
  File ".../scikit-learn/sklearn/utils/_set_output.py", line 316, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
  File ".../scikit-learn/sklearn/pipeline.py", line 1970, in fit_transform
    return self._hstack(Xs)
           ~~~~~~~~~~~~^^^^
  File ".../scikit-learn/sklearn/pipeline.py", line 2042, in _hstack
    return adapter.hstack(Xs)
           ~~~~~~~~~~~~~~^^^^
  File ".../scikit-learn/sklearn/utils/_set_output.py", line 183, in hstack
    return pl.concat(Xs, how="horizontal")
           ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.13/site-packages/polars/functions/eager.py", line 254, in concat
    out = wrap_df(plr.concat_df_horizontal(elems))
                  ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
polars.exceptions.DuplicateError: column with name 'a' has more than one occurrence
```

### Versions

```shell
System:
    python: 3.13.7 | packaged by conda-forge | (main, Sep  3 2025, 14:24:46) [Clang 19.1.7 ]
   machine: macOS-15.6.1-arm64-arm-64bit-Mach-O

Python dependencies:
      sklearn: 1.8.dev0
          pip: 25.2
   setuptools: None
        numpy: 2.3.2
        scipy: 1.16.1
       Cython: 3.1.3
       pandas: 2.3.2
   matplotlib: None
       joblib: 1.5.2
threadpoolctl: 3.6.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
    num_threads: 4
         prefix: libomp
       filepath: /opt/homebrew/Cellar/libomp/20.1.7/lib/libomp.dylib
        version: None
```

COMMENT:
Hello,

I would like to request to be assigned this issue regarding FeatureUnion concatenating outputs in Polars, which leads to DuplicateError due to duplicate column names.

Problem:
When FeatureUnion concatenates outputs before the set_output wrapper renames columns, the resulting Polars dataframe can have duplicate column names (e.g., multiple columns named 'a'). This works fine in pandas but raises an error in Polars, which does not allow duplicate column names.

Proposed Solution:
We can ensure that column names are prefixed with the transformer name before concatenating in Polars, similar to how get_feature_names_out works in pandas.

Example:

# Before (Polars)
Xs = [output1, output2]  # Both have columns ['a', 'b']
pl.concat(Xs, how="horizontal")  # ERROR: DuplicateError

# After
Xs_renamed = [output1.rename({c: f"std_1__{c}" for c in output1.columns}),
              output2.rename({c: f"std_2__{c}" for c in output2.columns})]
pl.concat(Xs_renamed, how="horizontal")  # OK: columns ['std_1__a', 'std_1__b', 'std_2__a', 'std_2__b']


Benefits:

Respects Polars’ no-duplicate-column constraint.

Provides consistent output naming with pandas.

Prevents runtime errors for users switching to Polars.

I am happy to implement this solution, add tests, and submit a PR.

Thank you for considering my request!

Labels: Bug