@@ -168,6 +168,8 @@ def _map(f, arr, na_mask=False, na_value=np.nan, dtype=object):
168
168
convert = not all (mask )
169
169
result = lib .map_infer_mask (arr , f , mask .view (np .uint8 ), convert )
170
170
except (TypeError , AttributeError ) as e :
171
+ # Reraise the exception if callable `f` got wrong number of args.
172
+ # The user may want to be warned by this, instead of getting NaN
171
173
re_missing = (r'missing \d+ required (positional|keyword-only) '
172
174
'arguments?' )
173
175
re_takes = (r'takes (from)?\d+ (to \d+)?positional arguments? '
@@ -311,8 +313,12 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
311
313
pat : string
312
314
Character sequence or regular expression
313
315
repl : string or callable
314
- Replacement string or a callable, it's passed the match object and
315
- must return a replacement string to be used. See :func:`re.sub`.
316
+ Replacement string or a callable. The callable is passed the regex
317
+ match object and must return a replacement string to be used.
318
+ See :func:`re.sub`.
319
+
320
+ .. versionadded:: 0.20.0
321
+
316
322
n : int, default -1 (all)
317
323
Number of replacements to make from start
318
324
case : boolean, default True
@@ -323,11 +329,52 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
323
329
Returns
324
330
-------
325
331
replaced : Series/Index of objects
332
+
333
+ Examples
334
+ --------
335
+ When ``repl`` is a string, every ``pat`` is replaced as with
336
+ :meth:`str.replace`. NaN value(s) in the Series are left as is.
337
+
338
+ >>> Series(['foo', 'fuz', np.nan]).str.replace('f', 'b')
339
+ 0 boo
340
+ 1 buz
341
+ 2 NaN
342
+ dtype: object
343
+
344
+ When ``repl`` is a callable, it is called on every ``pat`` using
345
+ :func:`re.sub`. The callable should expect one positional argument
346
+ (a regex object) and return a string.
347
+
348
+ To get the idea:
349
+
350
+ >>> Series(['foo', 'fuz', np.nan]).str.replace('f', repr)
351
+ 0 <_sre.SRE_Match object; span=(0, 1), match='f'>oo
352
+ 1 <_sre.SRE_Match object; span=(0, 1), match='f'>uz
353
+ 2 NaN
354
+ dtype: object
355
+
356
+ Reverse every lowercase alphabetic word:
357
+
358
+ >>> repl = lambda m: m.group(0)[::-1]
359
+ >>> Series(['foo 123', 'bar baz', np.nan]).str.replace(r'[a-z]+', repl)
360
+ 0 oof 123
361
+ 1 rab zab
362
+ 2 NaN
363
+ dtype: object
364
+
365
+ Using regex groups:
366
+
367
+ >>> pat = r"(?P<one>\w+) (?P<two>\w+) (?P<three>\w+)"
368
+ >>> repl = lambda m: m.group('two').swapcase()
369
+ >>> Series(['Foo Bar Baz', np.nan]).str.replace(pat, repl)
370
+ 0 bAR
371
+ 1 NaN
372
+ dtype: object
326
373
"""
327
374
328
- # Check whether repl is valid (GH 13438)
375
+ # Check whether repl is valid (GH 13438, GH 15055 )
329
376
if not (is_string_like (repl ) or callable (repl )):
330
- raise TypeError ("repl must be a string or function " )
377
+ raise TypeError ("repl must be a string or callable " )
331
378
use_re = not case or len (pat ) > 1 or flags or callable (repl )
332
379
333
380
if use_re :
0 commit comments