Skip to content

In encoders, switch to faster string representation than object dtype #34383

Open

Description

@itamarst

Right now, sklearn.preprocessing._encoders.BaseEncoder._check_X has on its list of tasks "convert list of strings to object dtype". This is bad from a performance perspective, since:

  1. You now have a pile of Python objects, and end up doing a lot of Python operations.
  2. Interactions that involve missing data are extra expensive; I'm going to submit a separate issue/PR to speed one code path, but there may be others.

There are two superior alternatives available, that would allow bypassing Python altogether:

  • NumPy's StringDType, with custom support for missing data - you can avoid using annoying nans, for example. Would require NumPy 2.0, but possibly a newer version if sklearn hits any bugs that were fixed in subsequent versions.
  • Arrow string arrays, with Arrow's built-in support for missing data. Would require PyArrow, or some other Arrow library.

As an alternative to just switching completely internally, it would also be possible to add support for one, the other, or both in addition to object-based dtypes. Then users who have sufficiently new NumPy or PyArrow already installed would benefit from faster runtime, while still preserving backwards compatibility.

Activity

  1. added
    Needs Triage
    on Jun 24, 2026
  2. cakedev0 commented on Jun 26, 2026

    @cakedev0
    Member

    Hi, I think I have a good solution to propose: preserve categorical series/dtypes of dataframes (which are the natural input of encoders). See #34393 and let me know what you think of the idea.

  3. itamarst commented on Jun 26, 2026

    @itamarst
    ContributorAuthor

    Oh, that's probably even better than strings, yeah. I'll take a look next week.

  4. ogrisel commented on Jul 8, 2026

    @ogrisel
    Member

    I think few people with pass list of strings for performance critical workloads, so I would not invest too much effort for the optimization of the "list of strings" case (and instead focus on improving dataframes with categorical dtypes as @cakedev0 suggested).

    Still supporting list of strings is useful for quick inline throw IPython experiments.

    That being said, adding native handling for NumPy 2.0 variable length strings to our encoders sounds good if someone is willing to do that work, but we should not expect to be able to depend on it. For the time being we still need scikit-learn to run with numpy 1.26 for the next release planned end of 2026:

    $ python maint_tools/bump-dependencies-versions.py 2026-12-01
    For future release at date 2026-12-01
    - python: 3.11 -> 3.12
    - numpy: 1.24.1 -> 1.26.0
    - scipy: 1.10.0 -> 1.12.0
    - pandas: 1.5.0 -> 2.2.0
    - matplotlib: 3.6.1 -> 3.8.0
    - pyamg: 5.0.0 -> 5.1.0
    - polars: 0.20.30 -> 1.16.0
    - pyarrow: 13.0.0 -> 14.0.0
    - narwhals: 2.0.1 -> 1.14.0
    - scikit-image: 0.22.0 -> 0.24.0
    - Pillow: 12.1.1 -> 11.0.0
    - plotly: 5.22.0 -> 5.24.0
    
  5. removed
    Needs Triage
    on Jul 8, 2026
  6. itamarst commented on Jul 8, 2026

    @itamarst
    ContributorAuthor

    I was thinking not as a replacement, you'd still keep current code as fallback. But anyone using newer libraries would get the speed benefit. OTOH direct categorical support is probably better.

  7. virchan commented on Sep 17, 2026

    @virchan
    Member

    There is also a broader discussion about supporting StringDType in #34946.

  8. lorentzenchr commented on Sep 28, 2026

    @lorentzenchr
    Member

    My order of preference is:

    • use narwhals
    • use native pandas/polars/pyarrow but no hard dependency on those
    • prefer the new StringDType
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions