Skip to content

FEAT Preserve DataFrame inputs and feature names in RFE and RFECV - #34789

Merged
ogrisel merged 12 commits into
scikit-learn:mainfrom
Kaynup:fix-rfe-dataframes-28629
Oct 13, 2026
Merged

ogrisel merged 12 commits into
scikit-learn:mainfrom
Kaynup:fix-rfe-dataframes-28629

Conversation

@Kaynup

@Kaynup Kaynup commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #28629

What does this implement/fix? Explain your changes.

Currently, RFE._fit and RFECV.fit coerce all input data to dense NumPy float arrays, which drops pandas DataFrame container information, column names, and categorical dtypes (such as category columns intended for estimators like HistGradientBoostingClassifier(categorical_features="from_dtype") or XGBoost). Furthermore, hardcoded array slicing X[:, features] causes errors on DataFrames.

This PR implements the approach agreed upon in the issue discussion:

  1. Aligns RFE._fit and RFECV.fit data validation with SelectorMixin.transform by preserving DataFrame inputs (skip_check_array=preserve_X where preserve_X = nw.dependencies.is_pandas_dataframe(X)).
  2. Replaces all array-specific indexing X[:, features] with _safe_indexing(X, features, axis=1) across _rfe_single_fit, RFE._fit, and RFECV.fit.
  3. Adds automated unit tests in sklearn/feature_selection/tests/test_rfe.py (test_rfe_pandas_dataframe_preservation and test_rfecv_pandas_dataframe_preservation) verifying feature name propagation and .predict() execution on DataFrames using RandomForestClassifier as the estimator.

Introduce yourself

I am a software developer and machine learning enthusiast, making my first contribution to scikit-learn. I love using scikit-learn and wanted to contribute by helping resolve open feature requests.

AI usage disclosure

I used AI assistance for:

  • Code generation (to align to native utils tools in sklearn.utils)
  • Reviewing my changes
  • Tests generation

Any other comments?

  • All 61 tests in sklearn/feature_selection/tests/test_rfe.py pass cleanly.
  • This is my first PR to scikit-learn, so please let me know if there are any formatting, conventions, or improvements I should make! Happy to iterate based on your feedback.

@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.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Aug 21, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Aug 21, 2026
Kaynup added 2 commits August 21, 2026 18:01
Previously, RFE's inference methods (predict, score, decision_function, etc.) called `self.transform(X)`. This inherently drops the Pandas DataFrame wrapper and returns a NumPy array unless explicitly overridden by `set_output(transform='pandas')`.

Since `RFE._fit` was updated to preserve the Pandas DataFrame, the underlying estimator was trained with feature names. When `predict` later passed a stripped NumPy array to the underlying estimator, it caused a UserWarning ('X does not have valid feature names, but estimator was fitted with feature names').

This commit introduces an internal `_transform_for_estimator(X)` helper. It validates and sub-indexes the DataFrame while retaining the Pandas container, ensuring the underlying estimator receives consistent data structures during both fit and inference.
@AnneBeyer

Copy link
Copy Markdown
Member

Thank you for the PR @Kaynup! We are currently short on reviewers, maybe @Gabriel-Kissin or @cleboo could help with reviewing this, as they were involved in the issue?

@ogrisel ogrisel 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.

Thanks for the PR. Here is a pass of review.

Comment thread sklearn/feature_selection/_rfe.py Resolved
Comment thread sklearn/feature_selection/_rfe.py Resolved
Comment thread sklearn/feature_selection/_rfe.py Resolved
Comment thread sklearn/feature_selection/_rfe.py Resolved
Comment thread sklearn/feature_selection/_rfe.py Resolved
Comment thread sklearn/feature_selection/_rfe.py Resolved
Kaynup added 2 commits August 27, 2026 15:21
- Set skip_check_array=True across RFE and RFECV
- Remove redundant validation parameters
- Unify and parametrize unit tests for RFE and RFECV across pandas and polars
- Use is_df_or_series from sklearn.utils._dataframe to preserve any DataFrame container (pandas, polars, pyarrow)
- Convert plain Python lists to ndarrays for 2D slicing compatibility
- Unify and parametrize unit tests for RFE and RFECV across pandas and polars using clone
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Aug 27, 2026
@Kaynup

Kaynup commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Actually, could you even try to run skip_check_array=True instead?
I think we can let the inner estimator handle the input validation details.

I tested setting skip_check_array=True unconditionally, but found that when inputs are raw Python lists (as tested in test_rfe_sparse_coef), skipping array check leaves X as a Python list without .shape or 2D column slicing support, causing an AttributeError.
While keeping Python lists and imputer pipelines supported:

  • preserve_X = is_df_or_series(X) from sklearn.utils._dataframe to preserve any DataFrame (pandas, polars, pyarrow) without special-casing pandas, while converting Python lists to ndarrays.
  • the other validations of nan and inf were clashing with other tests like test_pipeline_nans
    The particular fix was done by AI, I can check for other alternatives if you want to keep skip_array_check=True or if you can suggest something.

I did the rest of the changes as you suggested

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Aug 27, 2026
@Kaynup
Kaynup marked this pull request as draft August 27, 2026 17:47

@ogrisel ogrisel 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. Thanks for the follow-up.

@ogrisel ogrisel added the Quick Review For PRs that are quick to review label Aug 28, 2026
@Kaynup
Kaynup marked this pull request as ready for review August 31, 2026 11:21
@ogrisel
ogrisel merged commit 1a6cfd9 into scikit-learn:main Oct 13, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:feature_selection Quick Review For PRs that are quick to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make RFE/RFECV preserve pandas dataframes

3 participants