Skip to content

Commit 14beb21

Browse files
author
Joost Kranendonk
authored
Add test for .str.replace with regex named groups
1 parent 27065a2 commit 14beb21

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

pandas/tests/test_strings.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def test_replace(self):
436436
values = klass(data)
437437
self.assertRaises(TypeError, values.str.replace, 'a', repl)
438438

439-
## GH 15055, callable repl
439+
## GH 15055
440440
values = Series(['fooBAD__barBAD', NA])
441441

442442
# test with callable
@@ -445,10 +445,20 @@ def test_replace(self):
445445
exp = Series(['foObaD__baRbaD', NA])
446446
tm.assert_series_equal(result, exp)
447447

448-
# test with wrong type
449-
repl = lambda m: m.group(0).swapcase()
450-
result = values.str.replace('[a-z][A-Z]{2}', repl, n=2)
451-
exp = Series(['foObaD__baRbaD', NA])
448+
# test with wrong number of arguments
449+
repl = lambda m, bad: None
450+
re_msg = "^<lambda>\(\) missing 1 required positional argument: 'bad'$"
451+
self.assertRaisesRegex(TypeError, re_msg, values.str.replace, 'a', repl)
452+
453+
repl = lambda: None
454+
re_msg = '^<lambda>\(\) takes 0 positional arguments but 1 was given$'
455+
self.assertRaisesRegex(TypeError, re_msg, values.str.replace, 'a', repl)
456+
457+
# test regex named groups
458+
values = Series(['Foo Bar Baz', NA])
459+
repl = lambda m: m.group('middle').swapcase()
460+
result = values.str.replace(r"(?P<first>\w+) (?P<middle>\w+) (?P<last>\w+)", repl)
461+
exp = Series(['bAR', NA])
452462
tm.assert_series_equal(result, exp)
453463

454464
def test_repeat(self):

0 commit comments

Comments
 (0)