Skip to content

Allow categories parameter in OrdinalEncoder to accept a dict of column names → categories #32152

Open

Description

@joaosferreira

Describe the workflow you want to enable

Currently, the categories parameter in OrdinalEncoder only accepts:

  • "auto", or
  • a list of lists, where the position of each list corresponds to the order of columns in the input.

This makes it somewhat inconvenient when working with pandas DataFrames, since one must manually align the category lists with the column order.

Describe your proposed solution

Allow categories to also accept a dictionary mapping column names to category lists. For example:

encoder = OrdinalEncoder(categories={
    "size": ["small", "medium", "large"],
    "priority": ["low", "medium", "high"]
})

Describe alternatives you've considered, if relevant

No response

Additional context

Motivation

  • Improves ergonomics when working with pandas (very common in scikit-learn pipelines).
  • Reduces potential bugs from mismatched column ordering.
  • Makes the API consistent with the way many users already think about preprocessing (column → transformation).

Activity

  1. Yousinator commented on Sep 11, 2025

    @Yousinator

    Hey there.
    This sounds like a useful addition, for pandas workflows. If the proposal is accepted, I’d be happy to take on the implementation and open a PR.

  2. ogrisel commented on Sep 11, 2025

    @ogrisel
    Member

    I agree, this sounds like a very useful addition. @joaosferreira did you intend to open a PR yourself for it? Otherwise, @Yousinator could give it a shot.

  3. removed
    Needs Triage
    on Sep 11, 2025
  4. joaosferreira commented on Sep 11, 2025

    @joaosferreira
    Author

    Yes, I do. Already working on a draft. Is that okay @ogrisel and @Yousinator?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions