@@ -306,7 +306,7 @@ def str_endswith(arr, pat, na=np.nan):
306
306
return _na_map (f , arr , na , dtype = bool )
307
307
308
308
309
- def str_replace (arr , pat , repl , n = - 1 , case = None , flags = 0 ):
309
+ def str_replace (arr , pat , repl , n = - 1 , case = None , flags = 0 , regex = True ):
310
310
r"""
311
311
Replace occurrences of pattern/regex in the Series/Index with
312
312
some other string. Equivalent to :meth:`str.replace` or
@@ -337,25 +337,50 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0):
337
337
flags : int, default 0 (no flags)
338
338
- re module flags, e.g. re.IGNORECASE
339
339
- Cannot be set if `pat` is a compiled regex
340
+ regex : boolean, default True
341
+ - If True, assumes the passed-in pattern is a regular expression.
342
+ - If False, treats the pattern as a literal string
343
+ - Cannot be set to False if `pat` is a compiled regex or `repl` is
344
+ a callable.
345
+
346
+ .. versionadded:: 0.23.0
340
347
341
348
Returns
342
349
-------
343
350
replaced : Series/Index of objects
344
351
352
+ Raises
353
+ ------
354
+ ValueError
355
+ * if `regex` is False and `repl` is a callable or `pat` is a compiled
356
+ regex
357
+ * if `pat` is a compiled regex and `case` or `flags` is set
358
+
345
359
Notes
346
360
-----
347
361
When `pat` is a compiled regex, all flags should be included in the
348
- compiled regex. Use of `case` or `flags ` with a compiled regex will
349
- raise an error.
362
+ compiled regex. Use of `case`, `flags`, or `regex=False ` with a compiled
363
+ regex will raise an error.
350
364
351
365
Examples
352
366
--------
353
- When `repl` is a string, every `pat` is replaced as with
354
- :meth:`str.replace`. NaN value(s) in the Series are left as is.
367
+ When `pat` is a string and `regex` is True (the default), the given `pat`
368
+ is compiled as a regex. When `repl` is a string, it replaces matching
369
+ regex patterns as with :meth:`re.sub`. NaN value(s) in the Series are
370
+ left as is:
371
+
372
+ >>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f.', 'ba', regex=True)
373
+ 0 bao
374
+ 1 baz
375
+ 2 NaN
376
+ dtype: object
355
377
356
- >>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', 'b')
357
- 0 boo
358
- 1 buz
378
+ When `pat` is a string and `regex` is False, every `pat` is replaced with
379
+ `repl` as with :meth:`str.replace`:
380
+
381
+ >>> pd.Series(['f.o', 'fuz', np.nan]).str.replace('f.', 'ba', regex=False)
382
+ 0 bao
383
+ 1 fuz
359
384
2 NaN
360
385
dtype: object
361
386
@@ -397,34 +422,41 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0):
397
422
1 bar
398
423
2 NaN
399
424
dtype: object
425
+
400
426
"""
401
427
402
428
# Check whether repl is valid (GH 13438, GH 15055)
403
429
if not (is_string_like (repl ) or callable (repl )):
404
430
raise TypeError ("repl must be a string or callable" )
405
431
406
432
is_compiled_re = is_re (pat )
407
- if is_compiled_re :
408
- if ( case is not None ) or ( flags != 0 ) :
409
- raise ValueError ( " case and flags cannot be set"
410
- " when pat is a compiled regex" )
411
- else :
412
- # not a compiled regex
413
- # set default case
414
- if case is None :
415
- case = True
416
-
417
- # add case flag, if provided
418
- if case is False :
419
- flags |= re . IGNORECASE
420
-
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
- regex = re . compile ( pat , flags = flags )
426
- f = lambda x : regex . sub ( repl = repl , string = x , count = n )
433
+ if regex :
434
+ if is_compiled_re :
435
+ if ( case is not None ) or ( flags != 0 ):
436
+ raise ValueError ( "case and flags cannot be set"
437
+ " when pat is a compiled regex" )
438
+ else :
439
+ # not a compiled regex
440
+ # set default case
441
+ if case is None :
442
+ case = True
443
+
444
+ # add case flag, if provided
445
+ if case is False :
446
+ flags |= re . IGNORECASE
447
+ if is_compiled_re or len (pat ) > 1 or flags or callable (repl ):
448
+ n = n if n >= 0 else 0
449
+ compiled = re . compile ( pat , flags = flags )
450
+ f = lambda x : compiled . sub ( repl = repl , string = x , count = n )
451
+ else :
452
+ f = lambda x : x . replace ( pat , repl , n )
427
453
else :
454
+ if is_compiled_re :
455
+ raise ValueError ("Cannot use a compiled regex as replacement "
456
+ "pattern with regex=False" )
457
+ if callable (repl ):
458
+ raise ValueError ("Cannot use a callable replacement when "
459
+ "regex=False" )
428
460
f = lambda x : x .replace (pat , repl , n )
429
461
430
462
return _na_map (f , arr )
@@ -1596,9 +1628,9 @@ def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=None):
1596
1628
return self ._wrap_result (result )
1597
1629
1598
1630
@copy (str_replace )
1599
- def replace (self , pat , repl , n = - 1 , case = None , flags = 0 ):
1631
+ def replace (self , pat , repl , n = - 1 , case = None , flags = 0 , regex = True ):
1600
1632
result = str_replace (self ._data , pat , repl , n = n , case = case ,
1601
- flags = flags )
1633
+ flags = flags , regex = regex )
1602
1634
return self ._wrap_result (result )
1603
1635
1604
1636
@copy (str_repeat )
0 commit comments