Skip to content
Merged
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
revert changes, fix docstring
  • Loading branch information
rhshadrach committed May 9, 2021
commit e65bea7be464150fd0ace14d30316ed7df68ca90
17 changes: 7 additions & 10 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):

To get the idea:

>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', repr)
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', repr, regex=True)
0 <re.Match object; span=(0, 1), match='f'>oo
1 <re.Match object; span=(0, 1), match='f'>uz
2 NaN
Expand All @@ -1296,7 +1296,8 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):
Reverse every lowercase alphabetic word:

>>> repl = lambda m: m.group(0)[::-1]
>>> pd.Series(['foo 123', 'bar baz', np.nan]).str.replace(r'[a-z]+', repl)
>>> ser = pd.Series(['foo 123', 'bar baz', np.nan])
>>> ser.str.replace(r'[a-z]+', repl, regex=True)
0 oof 123
1 rab zab
2 NaN
Expand All @@ -1306,7 +1307,8 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):

>>> pat = r"(?P<one>\w+) (?P<two>\w+) (?P<three>\w+)"
>>> repl = lambda m: m.group('two').swapcase()
>>> pd.Series(['One Two Three', 'Foo Bar Baz']).str.replace(pat, repl)
>>> ser = pd.Series(['One Two Three', 'Foo Bar Baz'])
>>> ser.str.replace(pat, repl, regex=True)
0 tWO
1 bAR
dtype: object
Expand All @@ -1315,18 +1317,14 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):

>>> import re
>>> regex_pat = re.compile(r'FUZ', flags=re.IGNORECASE)
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace(regex_pat, 'bar')
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace(regex_pat, 'bar', regex=True)
0 foo
1 bar
2 NaN
dtype: object
"""
if regex is None:
if (
isinstance(pat, str)
and not callable(repl)
and any(c in pat for c in ".+*|^$?[](){}\\")
):
if isinstance(pat, str) and any(c in pat for c in ".+*|^$?[](){}\\"):
# warn only in cases where regex behavior would differ from literal
msg = (
"The default value of regex will change from True to False "
Expand All @@ -1338,7 +1336,6 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):
"*not* be treated as literal strings when regex=True."
)
warnings.warn(msg, FutureWarning, stacklevel=3)
# GH 41397 - When regex default is False, ignore regex when repl is callable
regex = True

# Check whether repl is valid (GH 13438, GH 15055)
Expand Down