Skip to content

ENH Support NumPy StringDType in validation and categorical encoding - #35082

Closed
lcrmorin wants to merge 9 commits into
scikit-learn:mainfrom
lcrmorin:stringdtype-cache-34946-clean
Closed

lcrmorin wants to merge 9 commits into
scikit-learn:mainfrom
lcrmorin:stringdtype-cache-34946-clean

Conversation

@lcrmorin

@lcrmorin lcrmorin commented Oct 6, 2026 •

Copy link
Copy Markdown

Reference Issues/PRs

Addresses #34946.

What does this implement/fix? Explain your changes.

Add support for NumPy StringDType.

Main issues were:

  • Improper dtype detection and subsequent improper handling (StringDType being sent to numeric estimator and not being sent to some categorical estimators).
  • Type-metadata attachment not working for StringDType. Solution was to have a generic cache approach. Feels a bit weird this is not handled on numpy side.
  • Then there were specific issues regarding missing values, unique values counting, and some interactions between those two issues.
  • There are two tests / benchmarks commit. I don't know to what extent this should be kept.

Detailed changes

Line counts include additions (+) and removals (−). Test fixtures count as tests.

Commit Explanation Implementation Tests Benchmarks / docs
b1d4269 Recognize StringDType while keeping numeric validation strict. +25 / −5 +65 / −0 —
8e13e5e Allow validation when dtype metadata is unavailable. +27 / −22 +88 / −1 —
4bef90e Support StringDType label and category encoding. +23 / −10 +149 / −2 —
ecbb0fa Preserve missing values, counts and inverse indices. +34 / −3 +88 / −0 —
e2f520a Test StringDType through estimators and pipelines. — +375 / −37 —
2df2220 Cache unique values within one operation, with safe cleanup. +82 / −18 +150 / −0 —
c316dc5 Reuse cached values across validation and binarization. +68 / −49 +62 / −0 —
052d966 Add runtime and memory benchmarks. — — +397 benchmark, +96 docs
6c97f15 Add the release note. — — +5 docs

Overall the commits are relatively simple. I reviewed them all. Most of the volume is test / benchmark.

The tricky part would be the type meta data caching part. Proper working needed to be ensured. The cache expires after each read-only operation, including on exceptions and when Python contexts are copied. Inputs must not be mutated within a scope. Native NumPy discovery and existing string mapping are preserved; no automatic object conversion is introduced.

Validation and performance

  • Full fork CI passed on identical source and tests before history cleanup. Its only extra file change is the fork-specific test workflow, excluded here.
  • Focused coverage on the cleaned branch: NumPy 2.2.4 566 passed; 2.5.3 565 passed / 1 skipped; 1.26.4 361 passed / 205 skipped. These are parameterized case counts, not new test-function counts.
  • For 100,000 short StringDType values, ten uniqueness lookups improved from 529 to 53 ms on NumPy 2.2.4 and 30 to 3.3 ms on 2.5.3 after scoped caching. This match base string behaviour.
  • The complete categorical feature pipeline showed no meaningful speedup over proper categorical handling. I'd say this is the main con against this PR. We are accommodating custom Stringtype to behave like categorical when the main approach should be some new categorical type.

AI usage disclosure

I used AI assistance for code generation, test/benchmark generation, documentation, and research and understanding. Except for the commit messages in the first 25 commits local branch, all messages to other devs are rewritten.

This pull request includes code written with the assistance of AI.
The code changes were largely reviewed by a human.

Pair numeric validation and target-type detection with focused regression tests.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
Preserve the NumPy cache where supported and verify the uncached StringDType fallback.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
Include round trips, unknown and mixed representations, and long strings with explicit category order.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
Pair NaN validation and category discovery with sentinel, inverse-index, and count tests.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
Add metrics, model-selection, classification, ensemble and wrapper regression coverage without temporary expected failures.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
Include nested-scope, exception, thread, copied-context, mutation and Array API regression tests.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
Integrate the scoped cache into target checks and metrics, including value-preserving reshapes and regression tests.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
Include isolated workers, operation-stage measurements, object-conversion comparisons and memory interpretation guidance.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

This pull request includes code written with the assistance of AI. The code has not yet been reviewed by a human.

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, @lcrmorin.

I'm closing this and suggesting that the work be split into smaller PRs, for two reasons:

  1. It doesn't follow the plan we agreed on in #34946. The plan was a tests-only PR first, followed by small, separate PRs for the fixes. This PR updates 35 files, which makes it much harder to review than the same work split into pieces.

  2. The caching change in sklearn/utils/_unique.py introduces more complexity than we agreed to take on. A change like this needs its own discussion in the issue before implementation.

To be clear, this isn't a rejection of StringDType support; that's still wanted. I suggest starting with the small tests-only PR from the original plan.

Smaller PRs also tend to get reviewed much faster, which is why our Contributing Guide recommends starting with them. The existing branch can stay available as a reference, so none of the work is lost.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants