Skip to content

Commit b5cfaaa

Browse files
jorisvandenbosschejreback
authored andcommitted
DOC: clean-up docstring str.replace (GH15056)
xref pandas-dev#15056 Author: Joris Van den Bossche <[email protected]> Closes pandas-dev#15210 from jorisvandenbossche/doc-replace-callable and squashes the following commits: 811cc34 [Joris Van den Bossche] DOC: clean-up docstring str.replace (GH15056)
1 parent 84bc3b2 commit b5cfaaa

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

pandas/core/strings.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
318318
match object and must return a replacement string to be used.
319319
See :func:`re.sub`.
320320
321-
.. versionadded:: 0.20.0
321+
.. versionadded:: 0.20.0
322+
`repl` also accepts a callable.
322323
323324
n : int, default -1 (all)
324325
Number of replacements to make from start
@@ -333,22 +334,22 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
333334
334335
Examples
335336
--------
336-
When ``repl`` is a string, every ``pat`` is replaced as with
337+
When `repl` is a string, every `pat` is replaced as with
337338
:meth:`str.replace`. NaN value(s) in the Series are left as is.
338339
339-
>>> Series(['foo', 'fuz', np.nan]).str.replace('f', 'b')
340+
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', 'b')
340341
0 boo
341342
1 buz
342343
2 NaN
343344
dtype: object
344345
345-
When ``repl`` is a callable, it is called on every ``pat`` using
346+
When `repl` is a callable, it is called on every `pat` using
346347
:func:`re.sub`. The callable should expect one positional argument
347348
(a regex object) and return a string.
348349
349350
To get the idea:
350351
351-
>>> Series(['foo', 'fuz', np.nan]).str.replace('f', repr)
352+
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', repr)
352353
0 <_sre.SRE_Match object; span=(0, 1), match='f'>oo
353354
1 <_sre.SRE_Match object; span=(0, 1), match='f'>uz
354355
2 NaN
@@ -357,19 +358,19 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
357358
Reverse every lowercase alphabetic word:
358359
359360
>>> repl = lambda m: m.group(0)[::-1]
360-
>>> Series(['foo 123', 'bar baz', np.nan]).str.replace(r'[a-z]+', repl)
361+
>>> pd.Series(['foo 123', 'bar baz', np.nan]).str.replace(r'[a-z]+', repl)
361362
0 oof 123
362363
1 rab zab
363364
2 NaN
364365
dtype: object
365366
366-
Using regex groups:
367+
Using regex groups (extract second group and swap case):
367368
368369
>>> pat = r"(?P<one>\w+) (?P<two>\w+) (?P<three>\w+)"
369370
>>> repl = lambda m: m.group('two').swapcase()
370-
>>> Series(['Foo Bar Baz', np.nan]).str.replace(pat, repl)
371-
0 bAR
372-
1 NaN
371+
>>> pd.Series(['One Two Three', 'Foo Bar Baz']).str.replace(pat, repl)
372+
0 tWO
373+
1 bAR
373374
dtype: object
374375
"""
375376

0 commit comments

Comments
 (0)