Skip to content

Add support for NumPy StringDType #34946

Open

Description

@lcrmorin

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

  1. added
    Needs Triage
    on Sep 12, 2026
  2. shahd-elgouhary commented on Sep 14, 2026

    @shahd-elgouhary

    Can I take this issue, please?

  3. lcrmorin commented on Sep 14, 2026

    @lcrmorin
    Author

    I suspect this one is quite difficult... advanced topic affecting multiple places.

  4. virchan commented on Sep 15, 2026

    @virchan
    Member

    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 StringDType reaches 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.

  5. added
    Needs Reproducible Code
    and removed
    Needs Triage
    on Sep 15, 2026
  6. lcrmorin commented on Sep 15, 2026

    @lcrmorin
    Author

    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.

  7. virchan commented on Sep 17, 2026

    @virchan
    Member

    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.

  8. added and removed
    Needs Reproducible Code
    on Sep 17, 2026
  9. changed the title [-]Multiple issues for the numpy new-style DType / isdtype[/-] [+]`xp.isdtype` checks raise on NumPy `StringDType`[/+] on Sep 17, 2026
  10. added and removed on Sep 17, 2026
  11. lcrmorin commented on Sep 27, 2026

    @lcrmorin
    Author

    @virchan, the gaps go beyond xp.isdtype:

    • Numeric-only validation does not consistently reject StringDType strings.
    • Missing-value validation can miss a NaN sentinel.
    • Unique-value caching tries to attach unsupported dtype metadata.
    • Mixed StringDType/Unicode inputs can fail during label lookup.

    Should we track these here and address them in separate PRs?

  12. changed the title [-]`xp.isdtype` checks raise on NumPy `StringDType`[/-] [+]`type_of_target` raise on NumPy `StringDType`[/+] on Sep 28, 2026
  13. lorentzenchr commented on Sep 28, 2026

    @lorentzenchr
    Member

    I think we should support StringDType numpy arrays. Maybe start by extending the (already existing) tests to cover it (with xfail at the beginning).

  14. changed the title [-]`type_of_target` raise on NumPy `StringDType`[/-] [+]Add support for NumPy `StringDType`[/+] on Sep 29, 2026
  15. lcrmorin commented on Sep 29, 2026

    @lcrmorin
    Author

    @lorentzenchr yeah it is more generic that way. Great idea to start with tests and see what break.

  16. virchan commented on Sep 29, 2026

    @virchan
    Member

    We discussed this at this month's developer meeting. The consensus is that accepting StringDType as 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 return StringDType arrays 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 StringDType inputs, marked xfail where they currently fail (and skipped on numpy < 2.0, since our minimum is still 1.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.

  17. lcrmorin commented on Oct 1, 2026

    @lcrmorin
    Author

    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.

  18. lesteve commented on Oct 5, 2026

    @lesteve
    Member

    Both CI pipeline run, no PR was opened as I still have one under review.

    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.

  19. lcrmorin commented on Oct 7, 2026

    @lcrmorin
    Author

    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.

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