You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In encoders, switch to faster string representation than object dtype #34383
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:
You now have a pile of Python objects, and end up doing a lot of Python operations.
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.
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.
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:
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.
Activity
cakedev0 commentedon Jun 26, 2026
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.
itamarst commentedon Jun 26, 2026
Oh, that's probably even better than strings, yeah. I'll take a look next week.
ogrisel commentedon Jul 8, 2026
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:
itamarst commentedon Jul 8, 2026
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.
virchan commentedon Sep 17, 2026
There is also a broader discussion about supporting
StringDTypein #34946.lorentzenchr commentedon Sep 28, 2026
My order of preference is: