From aed2f8d73923968f7e24c33be5ef2295225fe3c3 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 9 May 2021 10:49:02 -0400 Subject: [PATCH 1/5] DEPR: Don't warn in str.replace when repl is callable --- pandas/core/strings/accessor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index f8df05a7022d1..52245cfaa4088 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -1322,7 +1322,11 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None): dtype: object """ if regex is None: - if isinstance(pat, str) and any(c in pat for c in ".+*|^$?[](){}\\"): + if ( + isinstance(pat, str) + and not callable(repl) + 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 " @@ -1334,6 +1338,7 @@ 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) + # When changing default False, do not use False when repl is callable regex = True # Check whether repl is valid (GH 13438, GH 15055) From ec6d60a24557400a9b05137e75502deba42b2524 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 9 May 2021 10:56:17 -0400 Subject: [PATCH 2/5] fixup --- pandas/core/strings/accessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 52245cfaa4088..beb33a4ae6e58 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -1338,7 +1338,7 @@ 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) - # When changing default False, do not use False when repl is callable + # When changing default to False, do not use False when repl is callable regex = True # Check whether repl is valid (GH 13438, GH 15055) From b98a53fc3e6670f3d9f8a0205aa6c030922fb6d5 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 9 May 2021 11:02:16 -0400 Subject: [PATCH 3/5] Add GH # --- pandas/core/strings/accessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index beb33a4ae6e58..7ac3fd73ec7d3 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -1338,7 +1338,7 @@ 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) - # When changing default to False, do not use False when repl is callable + # GH 41397 - When regex default is False, ignore regex when repl is callable regex = True # Check whether repl is valid (GH 13438, GH 15055) From e65bea7be464150fd0ace14d30316ed7df68ca90 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 9 May 2021 16:02:45 -0400 Subject: [PATCH 4/5] revert changes, fix docstring --- pandas/core/strings/accessor.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 7ac3fd73ec7d3..538cc8e7c7237 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -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 oo 1 uz 2 NaN @@ -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 @@ -1306,7 +1307,8 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None): >>> pat = r"(?P\w+) (?P\w+) (?P\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 @@ -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 " @@ -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) From 6d1fb94fd95bcbdbccfad7ae4820fb616543ebd3 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 9 May 2021 16:04:08 -0400 Subject: [PATCH 5/5] grammar --- pandas/core/strings/accessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 538cc8e7c7237..9f8c9fa2f0515 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -1231,7 +1231,7 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None): Regex module flags, e.g. re.IGNORECASE. Cannot be set if `pat` is a compiled regex. regex : bool, default True - Determines if assumes the passed-in pattern is a regular expression: + Determines if the passed-in pattern is a regular expression: - If True, assumes the passed-in pattern is a regular expression. - If False, treats the pattern as a literal string