Repository navigation
Description
Goal
Support NumPy StringDType inputs where scikit-learn already accepts string labels or categories, while keeping numeric-only validation strict.
A minimal example is:
import numpy as np
from sklearn.utils.multiclass import type_of_target
y = np.array(["a", "b", "a"], dtype=np.dtypes.StringDType())
type_of_target(y) # Expected: "binary"On NumPy 2.2.4, this fails because np.isdtype rejects StringDType. This particular example passes on NumPy 2.5.3, so the failures depend on the NumPy version.
Compatibility gaps found during investigation
- Type detection: older NumPy versions can raise before downstream operations are reached.
- Numeric validation:
check_array(..., dtype="numeric")should reject StringDType strings, including numeric-looking strings. Explicitly requested numeric conversion is a separate case. - Unique-value caching: attaching dtype metadata is unsupported for StringDType. Correctness should not depend on that optimization.
- Encoding and missing values: mixed StringDType/Unicode inputs and missing sentinels need focused coverage for lookup, unique values and counts.
These are separate problems; fixing type detection alone does not resolve them all.
Proposed next steps
Following the developer-meeting feedback below, start by extending existing tests, using xfail for reproduced failures and skipping StringDType cases on NumPy < 2.0. Then address the failures in small, separate PRs.
The first tests-only PR is #35094. It extends two existing tests: classification-target detection and numeric-only string rejection. It is a first step, not complete compatibility coverage. Some downstream failures are masked by type detection on older NumPy, but other tests can already exercise them independently or on newer NumPy.
The broader implementation in closed PR #35082 remains a reference. Performance/caching redesign would need a separate discussion.
Related: #34383, which proposes using StringDType internally in encoders.
Activity
shahd-elgouhary commentedon Sep 14, 2026
Can I take this issue, please?
lcrmorin commentedon Sep 14, 2026
I suspect this one is quite difficult... advanced topic affecting multiple places.
virchan commentedon Sep 15, 2026
Thank you for opening this issue, @lcrmorin, and for helping improve scikit-learn.
If I understand correctly, you need to use NumPy variable-width strings with scikit-learn.
Could you share a minimal end-to-end example showing where
StringDTypereaches a scikit-learn estimator or metric? The reproducer above demonstrates the failure clearly, but a concrete use case would help us determine whether the project should support this dtype directly, or whether converting to fixed-width strings would be sufficient.lcrmorin commentedon Sep 15, 2026
I encountered this in a downstream package using scikit-learn's target-type detection. Converting to fixed-width strings is a workaround, but introduces padding and requires choosing a sufficient width.
Supporting StringDType would also align with #34383, which proposes using it internally in encoders.
virchan commentedon Sep 17, 2026
Thank you for sharing the additional context, @lcrmorin.
In light of #34383, I think it would be useful to keep the general compatibility discussion here. I'll add the "RFC" label to invite other contributors to weigh in.
I’ll also cross-link this discussion in #34383.
[-]Multiple issues for the numpy new-style DType / isdtype[/-][+]`xp.isdtype` checks raise on NumPy `StringDType`[/+]lcrmorin commentedon Sep 27, 2026
@virchan, the gaps go beyond
xp.isdtype:Should we track these here and address them in separate PRs?
[-]`xp.isdtype` checks raise on NumPy `StringDType`[/-][+]`type_of_target` raise on NumPy `StringDType`[/+]lorentzenchr commentedon Sep 28, 2026
I think we should support StringDType numpy arrays. Maybe start by extending the (already existing) tests to cover it (with xfail at the beginning).
[-]`type_of_target` raise on NumPy `StringDType`[/-][+]Add support for NumPy `StringDType`[/+]lcrmorin commentedon Sep 29, 2026
@lorentzenchr yeah it is more generic that way. Great idea to start with tests and see what break.
virchan commentedon Sep 29, 2026
We discussed this at this month's developer meeting. The consensus is that accepting
StringDTypeas input is worthwhile, as long as it doesn't add too much complexity. It would also make the project more future-proof if pandas or polars ever returnStringDTypearrays by default.Thanks, @lcrmorin, for scoping the gaps above, and fixing the title for me. As @lorentzenchr suggested, a good first step is a PR that extends the existing tests to cover
StringDTypeinputs, markedxfailwhere they currently fail (and skipped onnumpy < 2.0, since our minimum is still1.26). That gives us the full list of what breaks. We can then fix the failures in small, separate PRs.A PR for the tests is welcome.
lcrmorin commentedon Oct 1, 2026
I added exploratory coverage on the tests branch.
The main failure groups are type detection, numeric validation, missing values, unique-value metadata and mixed string representations. On older NumPy, type detection masks some downstream failures; newer versions expose more directly.
I'd start with focused tests for detection and validation, then cover the remaining helpers rather than repeat the same failure across every estimator.
lesteve commentedon Oct 5, 2026
Draft PRs do not count into towards the limits so I would suggest you mark #34809 as draft, and then you will be able to open a PR on StringDtype handling, see blog post for more details.
lcrmorin commentedon Oct 7, 2026
Thanks for the feedback on #35082. I have narrowed the first step to #35094: two small extensions to existing tests, covering target-type detection and numeric-only string rejection, with no implementation changes.
On older NumPy, type detection masks some downstream failures; it does not block all further testing. We can add focused encoding, missing-value and metadata-fallback tests separately, then fix each problem in a small PR.
Would you prefer to review these two tests first, or include a few more existing-test extensions in the initial tests-only PR?
I have also shortened the issue description to summarize the gaps and the proposed sequence.