Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support mixed-type arrays in OneHotEncoder and OrdinalEncoder#34445

Open
ranykamel wants to merge 4 commits into
scikit-learn:mainfrom
ranykamel:fix-mixed-type-onehotencoding
Open

Support mixed-type arrays in OneHotEncoder and OrdinalEncoder#34445
ranykamel wants to merge 4 commits into
scikit-learn:mainfrom
ranykamel:fix-mixed-type-onehotencoding

Conversation

@ranykamel

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #11379.

What does this implement/fix? Explain your changes.

The current OneHotEncoder and OrdinalEncoder implementations does not support encoding arrays of mixed typing (e.g. ints and strings). PR #16713 partially addresses the issue by replacing an ambiguous comparison error with a more explicit message stating that encoders only support uniform typing. This PR adds an opt-in parameter, allow_mixed_types, to allow mixed type arrays to be encoded.

The core of the bug centers on the built-in sorted call used to order categories, which fails on mixed types that cannot compare. To address this, the sort is wrapped by a helper, _sort_uniques, which applies a key to group values by type (e.g. strings are sorted with strings, numbers with numbers) while maintaining the original values and their types. This is conditioned on the allow_mixed_types parameter being toggled True; if it is false, it reverts to current behavior.

By default, encoders operate on current behavior, relying on the user to explictly allow mixed type encoding by toggling the parameter. Because a mix of types could be a sign that data was loaded incorrectly (e.g. column of strings with an erroneous numeric entry), the current error is raised by default to alert the user, and the error message now points the user to using allow_mixed_types=True in the case that the mix is intentional.

First time contributor introduction

I am a Software Engineer and Masters student interested in learning about Machine Learning and Data Science. I've used sklearn frequently in my work and academic research. I chose this issue as I wanted to get my feet wet in open-source development and get a deeper understaning on one of the libraries I frequently use. I'm always learning and welcome any feedback or guidance!

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)

All outputs from AI assistance tools were reviewed and are understood by me. I take responsibility for the contents of this PR.

Any other comments?

@github-actions

github-actions Bot commented Jul 8, 2026

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.

@ranykamel ranykamel changed the title Fix mixed type onehotencoding Support mixed-type arrays in OneHotEncoder and OrdinalEncoder Jul 8, 2026
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.

OneHotEncoder doesn't handle columns with mix of string and int

1 participant