Skip to content

Commit 7929610

Browse files
add tests and replace escape
1 parent 84280cb commit 7929610

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Other API Changes
4040
Deprecations
4141
~~~~~~~~~~~~
4242

43-
- :func:`Series.str.replace`, the default regex for single character might be deprecated.
43+
- :func:`Series.str.replace`, when pat is single special regex character and regex is not defined, regex is by default False for now, but this might be deprecated in the future.
4444

4545

4646
.. _whatsnew_0250.prior_deprecations:

pandas/core/strings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0, regex=None):
583583
"regex=False")
584584
# if regex is default None, and a single special character is given
585585
# in pat, still take it as a literal, and raise the Future warning
586-
if regex is None and len(pat) == 1 and re.escape(pat) != pat:
586+
if regex is None and len(pat) == 1 and pat in list("[\^$.|?*+()]"):
587587
warnings.warn("'{}' is interpreted as a literal in ".format(pat) +
588588
"default, not regex. It will change in the future.",
589589
FutureWarning)

pandas/tests/test_strings.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,12 +3441,21 @@ def test_replace_single_pattern(self, regex, expected_array):
34413441
expected = Series(expected_array)
34423442
tm.assert_series_equal(result, expected)
34433443

3444-
def test_replace_without_specifying_regex_parameter(self):
3445-
values = Series(['a.c'])
3446-
# GH: 24804
3447-
# test future deprecation warning
3448-
with tm.assert_produces_warning(FutureWarning,
3449-
check_stacklevel=False):
3450-
result = values.str.replace('.', 'b')
3451-
expected = Series(['abc'])
3444+
@pytest.mark.parametrize("input_array, single_char, replace_char,"
3445+
"expect_array, warn", [
3446+
("a.c", ".", "b", "abc", True),
3447+
("a@c", "@", "at", "aatc", False)
3448+
])
3449+
def test_replace_warning_single_character(self, input_array,
3450+
single_char, replace_char,
3451+
expect_array, warn):
3452+
values = Series([input_array])
3453+
if warn:
3454+
with tm.assert_produces_warning(FutureWarning,
3455+
check_stacklevel=False):
3456+
result = values.str.replace(single_char, replace_char)
3457+
else:
3458+
result = values.str.replace(single_char, replace_char)
3459+
3460+
expected = Series([expect_array])
34523461
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)