@@ -305,7 +305,7 @@ def str_endswith(arr, pat, na=np.nan):
305
305
return _na_map (f , arr , na , dtype = bool )
306
306
307
307
308
- def str_replace (arr , pat , repl , n = - 1 , case = None , flags = 0 ):
308
+ def str_replace (arr , pat , repl , n = - 1 , case = None , flags = 0 , regex = True ):
309
309
r"""
310
310
Replace occurrences of pattern/regex in the Series/Index with
311
311
some other string. Equivalent to :meth:`str.replace` or
@@ -336,6 +336,11 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0):
336
336
flags : int, default 0 (no flags)
337
337
- re module flags, e.g. re.IGNORECASE
338
338
- Cannot be set if `pat` is a compiled regex
339
+ regex : boolean, default True
340
+ - If True, assumes the passed-in pattern is a regular expression.
341
+ - If False, treats the pattern as a literal string
342
+ - Cannot be set to False if `pat` is a compiled regex or `repl` is
343
+ a callable.
339
344
340
345
Returns
341
346
-------
@@ -344,17 +349,27 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0):
344
349
Notes
345
350
-----
346
351
When `pat` is a compiled regex, all flags should be included in the
347
- compiled regex. Use of `case` or `flags ` with a compiled regex will
348
- raise an error.
352
+ compiled regex. Use of `case`, `flags`, or `regex ` with a compiled regex
353
+ will raise an error.
349
354
350
355
Examples
351
356
--------
352
- When `repl` is a string, every `pat` is replaced as with
353
- :meth:`str.replace`. NaN value(s) in the Series are left as is.
357
+ When `pat` is a string and `regex` is False, every `pat` is replaced with
358
+ `repl` as with :meth:`str.replace`. NaN value(s) in the Series are left as
359
+ is.
354
360
355
- >>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', 'b')
356
- 0 boo
357
- 1 buz
361
+ >>> pd.Series(['f.o', 'fuz', np.nan]).str.replace('f.', 'ba', regex=False)
362
+ 0 bao
363
+ 1 fuz
364
+ 2 NaN
365
+ dtype: object
366
+
367
+ When `pat` is a string and `regex` is True, the given `pat` is compiled
368
+ as a regex. When `repl` is a string, it replaces matching regex patterns
369
+ literally as with :meth:`re.sub`:
370
+ >>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f.', 'ba', regex=True)
371
+ 0 bao
372
+ 1 baz
358
373
2 NaN
359
374
dtype: object
360
375
@@ -403,27 +418,33 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0):
403
418
raise TypeError ("repl must be a string or callable" )
404
419
405
420
is_compiled_re = is_re (pat )
406
- if is_compiled_re :
407
- if ( case is not None ) or ( flags != 0 ) :
408
- raise ValueError ( " case and flags cannot be set"
409
- " when pat is a compiled regex" )
410
- else :
411
- # not a compiled regex
412
- # set default case
413
- if case is None :
414
- case = True
415
-
416
- # add case flag, if provided
417
- if case is False :
418
- flags |= re . IGNORECASE
419
-
420
- use_re = is_compiled_re or len (pat ) > 1 or flags or callable (repl )
421
-
422
- if use_re :
423
- n = n if n >= 0 else 0
424
- regex = re . compile ( pat , flags = flags )
425
- f = lambda x : regex . sub ( repl = repl , string = x , count = n )
421
+ if regex :
422
+ if is_compiled_re :
423
+ if ( case is not None ) or ( flags != 0 ):
424
+ raise ValueError ( "case and flags cannot be set"
425
+ " when pat is a compiled regex" )
426
+ else :
427
+ # not a compiled regex
428
+ # set default case
429
+ if case is None :
430
+ case = True
431
+
432
+ # add case flag, if provided
433
+ if case is False :
434
+ flags |= re . IGNORECASE
435
+ if is_compiled_re or len (pat ) > 1 or flags or callable (repl ):
436
+ n = n if n >= 0 else 0
437
+ compiled = re . compile ( pat , flags = flags )
438
+ f = lambda x : compiled . sub ( repl = repl , string = x , count = n )
439
+ else :
440
+ f = lambda x : x . replace ( pat , repl , n )
426
441
else :
442
+ if is_compiled_re :
443
+ raise ValueError ("Cannot use a compiled regex as replacement "
444
+ "pattern with regex=False" )
445
+ if callable (repl ):
446
+ raise ValueError ("Cannot use a callable replacement when "
447
+ "regex=False" )
427
448
f = lambda x : x .replace (pat , repl , n )
428
449
429
450
return _na_map (f , arr )
@@ -1604,9 +1625,9 @@ def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=None):
1604
1625
return self ._wrap_result (result )
1605
1626
1606
1627
@copy (str_replace )
1607
- def replace (self , pat , repl , n = - 1 , case = None , flags = 0 ):
1628
+ def replace (self , pat , repl , n = - 1 , case = None , flags = 0 , regex = True ):
1608
1629
result = str_replace (self ._data , pat , repl , n = n , case = case ,
1609
- flags = flags )
1630
+ flags = flags , regex = regex )
1610
1631
return self ._wrap_result (result )
1611
1632
1612
1633
@copy (str_repeat )
0 commit comments