9
9
is_string_like ,
10
10
is_list_like ,
11
11
is_scalar ,
12
- is_integer )
12
+ is_integer ,
13
+ is_re )
13
14
from pandas .core .common import _values_from_object
14
15
15
16
from pandas .core .algorithms import take_1d
@@ -303,16 +304,20 @@ def str_endswith(arr, pat, na=np.nan):
303
304
return _na_map (f , arr , na , dtype = bool )
304
305
305
306
306
- def str_replace (arr , pat , repl , n = - 1 , case = True , flags = 0 ):
307
+ def str_replace (arr , pat , repl , n = - 1 , case = None , flags = None ):
307
308
"""
308
309
Replace occurrences of pattern/regex in the Series/Index with
309
310
some other string. Equivalent to :meth:`str.replace` or
310
311
:func:`re.sub`.
311
312
312
313
Parameters
313
314
----------
314
- pat : string
315
- Character sequence or regular expression
315
+ pat : string or compiled regex
316
+ String can be a character sequence or regular expression.
317
+
318
+ .. versionadded:: 0.20.0
319
+ `pat` also accepts a compiled regex.
320
+
316
321
repl : string or callable
317
322
Replacement string or a callable. The callable is passed the regex
318
323
match object and must return a replacement string to be used.
@@ -323,15 +328,23 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
323
328
324
329
n : int, default -1 (all)
325
330
Number of replacements to make from start
326
- case : boolean, default True
327
- If True, case sensitive
328
- flags : int, default 0 (no flags)
329
- re module flags, e.g. re.IGNORECASE
331
+ case : boolean, optional
332
+ - if False, case insensitive
333
+ - Must be None if `pat` is a compiled regex
334
+ flags : int, optional
335
+ - re module flags, e.g. re.IGNORECASE
336
+ - Must be None if `pat` is a compiled regex
330
337
331
338
Returns
332
339
-------
333
340
replaced : Series/Index of objects
334
341
342
+ Notes
343
+ -----
344
+ When `pat` is a compiled regex, all flags should be included in the
345
+ compiled regex. Use of `case` or `flags` with a compiled regex will
346
+ raise an error.
347
+
335
348
Examples
336
349
--------
337
350
When `repl` is a string, every `pat` is replaced as with
@@ -372,21 +385,45 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
372
385
0 tWO
373
386
1 bAR
374
387
dtype: object
388
+
389
+ Using a compiled regex with flags
390
+
391
+ >>> regex_pat = re.compile(r'FUZ', flags=re.IGNORECASE)
392
+ >>> pd.Series(['foo', 'fuz', np.nan]).str.replace(regex_pat, 'bar')
393
+ 0 foo
394
+ 1 bar
395
+ 2 NaN
396
+ dtype: object
375
397
"""
376
398
377
399
# Check whether repl is valid (GH 13438, GH 15055)
378
400
if not (is_string_like (repl ) or callable (repl )):
379
401
raise TypeError ("repl must be a string or callable" )
380
- use_re = not case or len (pat ) > 1 or flags or callable (repl )
381
402
382
- if use_re :
383
- if not case :
403
+ is_compiled_re = is_re (pat )
404
+ if is_compiled_re :
405
+ if (case is not None ) or (flags is not None ):
406
+ raise ValueError ("case and flags must be None"
407
+ " when pat is a compiled regex" )
408
+ flags = 0
409
+ else :
410
+ # not a compiled regex
411
+ # set default case/flags
412
+ if case is None :
413
+ case = True
414
+ if flags is None :
415
+ flags = 0
416
+
417
+ # add case flag, if provided
418
+ if case is False :
384
419
flags |= re .IGNORECASE
385
- regex = re .compile (pat , flags = flags )
386
- n = n if n >= 0 else 0
387
420
388
- def f (x ):
389
- return regex .sub (repl , x , count = n )
421
+ use_re = is_compiled_re or len (pat ) > 1 or flags or callable (repl )
422
+
423
+ if use_re :
424
+ n = n if n >= 0 else 0
425
+ f = lambda x : re .sub (pattern = pat , repl = repl , string = x ,
426
+ count = n , flags = flags )
390
427
else :
391
428
f = lambda x : x .replace (pat , repl , n )
392
429
@@ -1558,7 +1595,7 @@ def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=False):
1558
1595
return self ._wrap_result (result )
1559
1596
1560
1597
@copy (str_replace )
1561
- def replace (self , pat , repl , n = - 1 , case = True , flags = 0 ):
1598
+ def replace (self , pat , repl , n = - 1 , case = None , flags = None ):
1562
1599
result = str_replace (self ._data , pat , repl , n = n , case = case ,
1563
1600
flags = flags )
1564
1601
return self ._wrap_result (result )
0 commit comments