Skip to content

Commit 8159e8d

Browse files
authored
DOC: specify regex=True in str.replace (#41397)
1 parent 0e42c55 commit 8159e8d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pandas/core/strings/accessor.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):
12311231
Regex module flags, e.g. re.IGNORECASE. Cannot be set if `pat` is a compiled
12321232
regex.
12331233
regex : bool, default True
1234-
Determines if assumes the passed-in pattern is a regular expression:
1234+
Determines if the passed-in pattern is a regular expression:
12351235
12361236
- If True, assumes the passed-in pattern is a regular expression.
12371237
- 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):
12871287
12881288
To get the idea:
12891289
1290-
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', repr)
1290+
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', repr, regex=True)
12911291
0 <re.Match object; span=(0, 1), match='f'>oo
12921292
1 <re.Match object; span=(0, 1), match='f'>uz
12931293
2 NaN
@@ -1296,7 +1296,8 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):
12961296
Reverse every lowercase alphabetic word:
12971297
12981298
>>> repl = lambda m: m.group(0)[::-1]
1299-
>>> pd.Series(['foo 123', 'bar baz', np.nan]).str.replace(r'[a-z]+', repl)
1299+
>>> ser = pd.Series(['foo 123', 'bar baz', np.nan])
1300+
>>> ser.str.replace(r'[a-z]+', repl, regex=True)
13001301
0 oof 123
13011302
1 rab zab
13021303
2 NaN
@@ -1306,7 +1307,8 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):
13061307
13071308
>>> pat = r"(?P<one>\w+) (?P<two>\w+) (?P<three>\w+)"
13081309
>>> repl = lambda m: m.group('two').swapcase()
1309-
>>> pd.Series(['One Two Three', 'Foo Bar Baz']).str.replace(pat, repl)
1310+
>>> ser = pd.Series(['One Two Three', 'Foo Bar Baz'])
1311+
>>> ser.str.replace(pat, repl, regex=True)
13101312
0 tWO
13111313
1 bAR
13121314
dtype: object
@@ -1315,7 +1317,7 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=None):
13151317
13161318
>>> import re
13171319
>>> regex_pat = re.compile(r'FUZ', flags=re.IGNORECASE)
1318-
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace(regex_pat, 'bar')
1320+
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace(regex_pat, 'bar', regex=True)
13191321
0 foo
13201322
1 bar
13211323
2 NaN

0 commit comments

Comments
 (0)