Skip to content

FeatureUnion with polars output can error due to duplicate column names #32104

Closed

Description

@jeromedockes

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

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

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

Activity

  1. added
    Needs Triage
    on Sep 4, 2025
  2. jeremiedbb commented on Sep 4, 2025

    @jeremiedbb
    Member

    Thanks for the report @jeromedockes. The issue looks similar to #28260.

    A quick fix would be to rename the columns of all Xs before stacking them. We don't really need to care about the new names of the columns since the set_output wrapper will use the columns from get_feature_names_out afterwards anyway.

  3. removed
    Needs Triage
    on Sep 4, 2025
  4. jeromedockes commented on Sep 4, 2025

    @jeromedockes
    ContributorAuthor

    set_output wrapper will use the columns from get_feature_names_out afterwards anyway.

    indeed, because even if the transformers output dataframes, unless set_output is set they will be concatenated with numpy so the dataframes' column names will not be preserved in any case

  5. JawadAliAI commented on Sep 5, 2025

    @JawadAliAI
  6. ArafathJ commented on Sep 7, 2025

    @ArafathJ
  7. AtharvaAiren commented on Sep 7, 2025

    @AtharvaAiren
  8. jeremiedbb commented on Sep 16, 2025

    @jeremiedbb
    Member

    I found another unexpected behavior: The snippet above works for pandas even if we set verbose_feature_names_out=False. It should error if there are duplicate names. It does in ColumnTransformer. I updated #32106 to fix that as well (see #32106 (comment))

  9. jeremiedbb commented on Feb 2, 2026

    @jeremiedbb
    Member

    Fixed by #32853

  10. jeremiedbb commented on Feb 15, 2026

    @jeremiedbb
    Member

    Actually #32853 did not completely fixed the issue, and at the same time introduced a regression, because it did not take into account transformers that don't implement get_feature_names_out. I reopened #32106 which is a more robust fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions