Skip to content

Commit 0c4e02e

Browse files
committed
ENH: str.replace accepts a compiled expression
.str.replace now accepts a compiled regular expression. See #15446 TODO before merging: - Consider moving check for compiled regex to `pandas.types` and using a dummy compiled regex for type checking. - Consider what to do with `case` and `flags` parameters, which are noop when `pat` is a compiled regex. - Add compiled regex tests for `str_replace` - Add compiled regex documentation for `str_replace`
1 parent be4a63f commit 0c4e02e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ New features
2828

2929
- Integration with the ``feather-format``, including a new top-level ``pd.read_feather()`` and ``DataFrame.to_feather()`` method, see :ref:`here <io.feather>`.
3030
- ``.str.replace`` now accepts a callable, as replacement, which is passed to ``re.sub`` (:issue:`15055`)
31+
- ``.str.replace`` now accepts a compiled regular expression as pattern (:issue:`15446`)
3132

3233

3334

pandas/core/strings.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -377,22 +377,31 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
377377
# Check whether repl is valid (GH 13438, GH 15055)
378378
if not (is_string_like(repl) or callable(repl)):
379379
raise TypeError("repl must be a string or callable")
380-
use_re = not case or len(pat) > 1 or flags or callable(repl)
380+
is_re = isinstance(pat, re._pattern_type)
381+
build_re = not is_re and (
382+
not case or len(pat) > 1 or flags or callable(repl))
381383

382-
if use_re:
384+
if is_re:
385+
f = _str_replace_regex_func(pat, repl, n)
386+
elif build_re:
383387
if not case:
384388
flags |= re.IGNORECASE
385389
regex = re.compile(pat, flags=flags)
386-
n = n if n >= 0 else 0
387-
388-
def f(x):
389-
return regex.sub(repl, x, count=n)
390+
f = _str_replace_regex_func(regex, repl, n)
390391
else:
391392
f = lambda x: x.replace(pat, repl, n)
392393

393394
return _na_map(f, arr)
394395

395396

397+
def _str_replace_regex_func(regex, repl, n=-1):
398+
n = n if n >= 0 else 0
399+
400+
def f(x):
401+
return regex.sub(repl, x, count=n)
402+
return f
403+
404+
396405
def str_repeat(arr, repeats):
397406
"""
398407
Duplicate each string in the Series/Index by indicated number

0 commit comments

Comments
 (0)