Fix str.replace(..., n=0) replacing every occurrence instead of none - #11545
Open
Kropiunig wants to merge 3 commits into
Open
Fix str.replace(..., n=0) replacing every occurrence instead of none#11545Kropiunig wants to merge 3 commits into
Kropiunig wants to merge 3 commits into
Conversation
The regex branch of StringAccessor.replace passed count=max(n, 0) to re.sub, where count=0 means 'replace every occurrence'. That collapsed n=0 onto n=-1, so asking for zero replacements silently replaced all of them, while the regex=False branch (str.replace) honoured n=0 correctly. Special-case n=0 so both branches match the documented behaviour that n is the number of replacements to make from the start and only -1 means 'replace all'.
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
StringAccessor.replacedocumentsnas "Number of replacements to make from start. Use-1to replace all." The regex code path (the default,regex=True) forwardscount=max(n, 0)tore.Pattern.sub, andre.subtreatscount=0as replace every occurrence. Son=0collapses onton=-1and replaces everything instead of nothing — silently, with no error.The literal path (
regex=False) callsstr.replace(pat, repl, n), which honoursn=0correctly. The same call therefore gives two different answers depending onregex:This is most damaging where
nis array-like, which is a documented feature ("Ifpat,repl, ornis array-like, they are broadcast against the array and applied elementwise"). A single0in the broadcastnsilently becomes "replace all" for that element:Two further references agree that
n=0means "no replacements":pandas.Series.str.replace(..., n=0)is a no-op for bothregex=Trueandregex=False, and xarray's ownstr.split(..., maxsplit=0)already performs zero splits.The fix special-cases
n == 0in the regex branch. Negativenstill maps to "replace all" exactly as before, so no other behaviour changes.Added
test_replace_n_zero, which covers the regex path, the literal path and the broadcast case. It fails onmain(2 failed / 12 passed for-k replace) and passes with this change (14 passed); the fullxarray/tests/test_accessor_str.pymodule is 228 passed, 1 skipped.Checklist
whats-new.rstAI Disclosure
Tools: Claude Code. Prompt: audit the xarray source for a provably-correct, unclaimed bug with a user-visible symptom on the default code path, then produce a minimal fix plus a regression test that fails before and passes after.