Skip to content

Commit 09c1c9f

Browse files
Liam3851TomAugspurger
authored andcommitted
ENH: Allow literal (non-regex) replacement using .str.replace pandas-dev#16808
1 parent a98bd34 commit 09c1c9f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pandas/tests/test_strings.py

+14
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ def test_replace(self):
431431
values = klass(data)
432432
pytest.raises(TypeError, values.str.replace, 'a', repl)
433433

434+
# GH16808 literal replace (regex=False vs regex=True)
435+
values = Series(['f.o', 'foo', NA])
436+
exp = Series(['bao', 'bao', NA])
437+
result = values.str.replace('f.', 'ba')
438+
tm.assert_series_equal(result, exp)
439+
440+
exp = Series(['bao', 'foo', NA])
441+
result = values.str.replace('f.', 'ba', regex=False)
442+
tm.assert_series_equal(result, exp)
443+
434444
def test_replace_callable(self):
435445
# GH 15055
436446
values = Series(['fooBAD__barBAD', NA])
@@ -441,6 +451,8 @@ def test_replace_callable(self):
441451
exp = Series(['foObaD__baRbaD', NA])
442452
tm.assert_series_equal(result, exp)
443453

454+
pytest.raises(ValueError, values.str.replace, 'abc', repl, regex=False)
455+
444456
# test with wrong number of arguments, raising an error
445457
if compat.PY2:
446458
p_err = r'takes (no|(exactly|at (least|most)) ?\d+) arguments?'
@@ -522,6 +534,8 @@ def test_replace_compiled_regex(self):
522534
"case and flags cannot be"):
523535
result = values.str.replace(pat, '', case=True)
524536

537+
pytest.raises(ValueError, values.str.replace, pat, '', regex=False)
538+
525539
# test with callable
526540
values = Series(['fooBAD__barBAD', NA])
527541
repl = lambda m: m.group(0).swapcase()

0 commit comments

Comments
 (0)