Skip to content

FIX preserve pandas dataframes in SequentialFeatureSelector - #34457

Open
arham766 wants to merge 1 commit into
scikit-learn:mainfrom
arham766:sfs-preserve-dataframe-25711
Open

arham766 wants to merge 1 commit into
scikit-learn:mainfrom
arham766:sfs-preserve-dataframe-25711

Conversation

@arham766

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #25711

What does this implement/fix? Explain your changes.

SequentialFeatureSelector.fit converted X to a numpy array before slicing candidate feature subsets, so the underlying estimator never saw column names or dtypes. This made it impossible to use estimators that rely on them — notably a Pipeline containing a ColumnTransformer with callable column selectors, the pattern suggested in #25711 (comment).

This PR preserves pandas dataframes in fit via validate_data(..., skip_check_array=True) and slices candidate subsets with _safe_indexing(X, candidate_mask, axis=1), mirroring what SelectorMixin.transform already does to preserve dataframes. With this change the following works:

ct = make_column_transformer(
    (StandardScaler(), make_column_selector(dtype_include=np.number)),
    (OneHotEncoder(), make_column_selector(dtype_exclude=np.number)),
)
selector_pipe = make_pipeline(ct, LogisticRegression())
SequentialFeatureSelector(selector_pipe, n_features_to_select=2).fit(X, y)

ColumnTransformers configured with hard-coded positional indices still fail, as discussed in the issue — once features are subset, positional specs against the original X are inherently ambiguous; callable selectors (or refitting semantics) are the supported route.

An explicit check keeps the previous ensure_min_features=2 error message for dataframes. Numpy, sparse, and other inputs go through the exact same validation as before.

Any other comments?

Behaviour change for pandas input: the inner estimator is now fitted on dataframe slices (original dtypes, no forced float conversion, no upfront NaN check — the estimator's own validation applies, consistent with the general meta-estimator delegation direction). Happy to adjust if full validation should be retained for the non-ColumnTransformer case.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.
  • The pull request addresses an existing issue that is ready for
    contribution (e.g. not tagged as "Needs Triage", "Needs Decision", ...).
    If you are proposing a new feature, please open an issue to discuss it first.
  • There are no other open pull requests already targeting the same issue.
  • You have followed the pull request checklist.
    In particular, linting and tests should pass.

SequentialFeatureSelector converted dataframes to numpy arrays before
slicing candidate feature subsets, so estimators relying on column names
or dtypes (e.g. a pipeline with a ColumnTransformer using callable column
selectors) could not be used for the selection.

Skip the array conversion for pandas dataframes and slice candidate
subsets with _safe_indexing, mirroring SelectorMixin.transform.

Fixes scikit-learn#25711
@arham766
arham766 force-pushed the sfs-preserve-dataframe-25711 branch from 6ccabee to e8f347f Compare July 10, 2026 10:23

This branch has not been deployed

No deployments
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.

SequentialFeatureSelector is not working with ColumnTransformer

1 participant