diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index f8df05a7022d1..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 @@ -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,7 +1317,7 @@ 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