-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix for .str.replace with repl function #15056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
hzpc-joostk
wants to merge
14
commits into
pandas-dev:master
from
hzpc-joostk:pandas-GH15055-patch-1
Closed
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6ecc43d
BUG: Fix for .str.replace with repl function
91c883d
Update .str.replace docstring
4baa0a7
add tests for .str.replace with callable repl
30d4727
Bug fix in .str.replace type checking done wrong
067a7a8
Fix testing bug for .str.replace
ae04a3e
Add whatsnew for .str.replace with callable repl
27065a2
Reraise TypeError only with wrong number of args
14beb21
Add test for .str.replace with regex named groups
f15ee2a
improve test .str.replace with callable
40c0d97
improve .str.replace with callable
e15dcdf
fix bug catch TypeError with wrong number of args
c2cc13a
Update v0.20.0.txt
90779ce
fix linting issues
826730c
simplify .str.replace TypeError reraising and test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -435,6 +435,35 @@ def test_replace(self): | |
for data in (['a', 'b', None], ['a', 'b', 'c', 'ad']): | ||
values = klass(data) | ||
self.assertRaises(TypeError, values.str.replace, 'a', repl) | ||
|
||
def test_replace_callable(self): | ||
## GH 15055 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make a separate test |
||
values = Series(['fooBAD__barBAD', NA]) | ||
|
||
# test with callable | ||
repl = lambda m: m.group(0).swapcase() | ||
result = values.str.replace('[a-z][A-Z]{2}', repl, n=2) | ||
exp = Series(['foObaD__baRbaD', NA]) | ||
tm.assert_series_equal(result, exp) | ||
|
||
# test with wrong number of arguments | ||
repl = lambda m, bad: None | ||
re_msg = "^<lambda>\(\) missing 1 required positional argument: 'bad'$" | ||
with tm.assertRaisesRegexp(TypeError, re_msg): | ||
values.str.replace('a', repl) | ||
|
||
repl = lambda: None | ||
re_msg = '^<lambda>\(\) takes 0 positional arguments but 1 was given$' | ||
with tm.assertRaisesRegexp(TypeError, re_msg): | ||
values.str.replace('a', repl) | ||
|
||
# test regex named groups | ||
values = Series(['Foo Bar Baz', NA]) | ||
pat = r"(?P<first>\w+) (?P<middle>\w+) (?P<last>\w+)" | ||
repl = lambda m: m.group('middle').swapcase() | ||
result = values.str.replace(pat, repl) | ||
exp = Series(['bAR', NA]) | ||
tm.assert_series_equal(result, exp) | ||
|
||
def test_repeat(self): | ||
values = Series(['a', 'b', NA, 'c', NA, 'd']) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add some examples to the doc-string?
also would be nice to have an example in text.rst