Skip to content

Commit 6fe5e8f

Browse files
simplify the code
1 parent b7e62c2 commit 6fe5e8f

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

pandas/core/strings.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,6 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0, regex=None):
551551
raise TypeError("repl must be a string or callable")
552552

553553
is_compiled_re = is_re(pat)
554-
if not is_compiled_re and regex is None:
555-
if len(pat) == 1 and re.findall(r"\W", pat):
556-
regex = False
557-
warnings.warn(f"{pat} is interpreted as a literal in default, "
558-
f"not regex. The default will change in the future",
559-
FutureWarning)
560554
if regex:
561555
if is_compiled_re:
562556
if (case is not None) or (flags != 0):
@@ -584,6 +578,12 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0, regex=None):
584578
if callable(repl):
585579
raise ValueError("Cannot use a callable replacement when "
586580
"regex=False")
581+
# if regex is default None, and a single special character is given
582+
# in pat, still take it as a literal, and raise the Future warning
583+
if regex is None and len(pat) == 1 and re.findall(r"\W", pat):
584+
warnings.warn(f"{pat} is interpreted as a literal in default, "
585+
f"not regex. The default will change in the future",
586+
FutureWarning)
587587
f = lambda x: x.replace(pat, repl, n)
588588

589589
return _na_map(f, arr)

0 commit comments

Comments
 (0)