Skip to content

Use np.strings in the string accessor - #11746

Merged
headtr1ck merged 6 commits into
pydata:mainfrom
headtr1ck:str-accessor-np-strings
Oct 22, 2026
Merged

headtr1ck merged 6 commits into
pydata:mainfrom
headtr1ck:str-accessor-np-strings

Conversation

@headtr1ck

Copy link
Copy Markdown
Collaborator

Use the vectorized functions of np.strings in the string accessor, now that the minimum numpy version is 2.2 (as suggested in #11722 (review)).

  • len, startswith/endswith, find/rfind/index/rindex, strip, pad/center/ljust/rjust/zfill, repeat, the is* methods, and contains/replace with regex=False use np.strings for U, S and StringDType arrays; object arrays keep the elementwise path.

  • 5-80x faster for 100k strings (new StringAccessor asv benchmark), unchanged for object arrays.

  • Fixes results of e.g. strip, center and replace on dask arrays, which were truncated to a single character.

  • Methods that are not C ufuncs in numpy 2.2 (upper, lower, translate, slice, ...) or use regular expressions stay elementwise.

  • index on bytes now raises numpy's "substring not found" instead of "subsection not found".

  • Tests added

  • User visible changes (including notable bug fixes) are documented in whats-new.rst

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
    • Tools: Claude Code with Opus 5.5

🤖 Generated with Claude Code

headtr1ck and others added 3 commits October 16, 2026 13:10
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🏎️ New personal best! Some benchmarks got better.

Change Before [c52a59f] After [b8c6750] Ratio Benchmark (Parameter)
🚀 28.1±0.2ms 1.54±0.02ms 0.05 accessors.StringAccessor.time_center('S')
🚀 33.3±0.1ms 1.38±0.05ms 0.04 accessors.StringAccessor.time_center('U')
🚀 32.7±0.3ms 2.67±0.01ms 0.08 accessors.StringAccessor.time_contains('S')
🚀 16.4±0.04ms 2.86±0.01ms 0.17 accessors.StringAccessor.time_contains('U')
🚀 27.0±0.3ms 2.63±0.01ms 0.1 accessors.StringAccessor.time_find('S')
🚀 27.5±0.2ms 2.84±0.01ms 0.1 accessors.StringAccessor.time_find('U')
🚀 11.7±0.09ms 2.02±0.01ms 0.17 accessors.StringAccessor.time_isalpha('S')
🚀 16.7±0.08ms 3.59±0.01ms 0.21 accessors.StringAccessor.time_isalpha('U')
🚀 7.55±0.04ms 212±4μs 0.03 accessors.StringAccessor.time_len('S')
🚀 11.2±0.2ms 230±20μs 0.02 accessors.StringAccessor.time_len('U')
🚀 21.3±0.1ms 1.20±0.01ms 0.06 accessors.StringAccessor.time_repeat('S')
🚀 27.2±0.09ms 1.41±0.05ms 0.05 accessors.StringAccessor.time_repeat('U')
🚀 26.2±0.5ms 4.09±0.05ms 0.16 accessors.StringAccessor.time_replace('S')
🚀 27.1±1ms 4.56±0.05ms 0.17 accessors.StringAccessor.time_replace('U')
🚀 17.5±0.5ms 551±7μs 0.03 accessors.StringAccessor.time_startswith('S')
🚀 17.9±0.09ms 571±8μs 0.03 accessors.StringAccessor.time_startswith('U')
🚀 18.5±0.1ms 1.67±0.02ms 0.09 accessors.StringAccessor.time_strip('S')
🚀 23.5±0.3ms 1.82±0.04ms 0.08 accessors.StringAccessor.time_strip('U')

🚀 better · 🐌 worse · 💥 now failing · 🩹 fixed · 🤷 not comparable

Comparing b8c6750d (this PR merged into the base branch) with c52a59f9 (base branch).
Only changes by more than a factor of 1.5 are shown, see the workflow run for all results.

headtr1ck and others added 2 commits October 20, 2026 22:28
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headtr1ck headtr1ck added the plan to merge Final call for comments label Oct 20, 2026
@headtr1ck
headtr1ck enabled auto-merge (squash) October 22, 2026 11:45
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headtr1ck
headtr1ck merged commit 82ec34a into pydata:main Oct 22, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments topic-performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants