Skip to content

Commit

Permalink
ENH: Allow literal (non-regex) replacement using .str.replace pandas-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam3851 authored and TomAugspurger committed Feb 27, 2018
1 parent a98bd34 commit 09c1c9f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ def test_replace(self):
values = klass(data)
pytest.raises(TypeError, values.str.replace, 'a', repl)

# GH16808 literal replace (regex=False vs regex=True)
values = Series(['f.o', 'foo', NA])
exp = Series(['bao', 'bao', NA])
result = values.str.replace('f.', 'ba')
tm.assert_series_equal(result, exp)

exp = Series(['bao', 'foo', NA])
result = values.str.replace('f.', 'ba', regex=False)
tm.assert_series_equal(result, exp)

def test_replace_callable(self):
# GH 15055
values = Series(['fooBAD__barBAD', NA])
Expand All @@ -441,6 +451,8 @@ def test_replace_callable(self):
exp = Series(['foObaD__baRbaD', NA])
tm.assert_series_equal(result, exp)

pytest.raises(ValueError, values.str.replace, 'abc', repl, regex=False)

# test with wrong number of arguments, raising an error
if compat.PY2:
p_err = r'takes (no|(exactly|at (least|most)) ?\d+) arguments?'
Expand Down Expand Up @@ -522,6 +534,8 @@ def test_replace_compiled_regex(self):
"case and flags cannot be"):
result = values.str.replace(pat, '', case=True)

pytest.raises(ValueError, values.str.replace, pat, '', regex=False)

# test with callable
values = Series(['fooBAD__barBAD', NA])
repl = lambda m: m.group(0).swapcase()
Expand Down

0 comments on commit 09c1c9f

Please sign in to comment.