Skip to content

Commit d21e4f0

Browse files
committed
REGR: Don't ignore compiled patterns in replace
1 parent a0896e1 commit d21e4f0

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

doc/source/whatsnew/v1.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fixed regressions
2323
- Fixed regression in ``.groupby(..).rolling(..)`` where a segfault would occur with ``center=True`` and an odd number of values (:issue:`35552`)
2424
- Fixed regression in :meth:`DataFrame.apply` where functions that altered the input in-place only operated on a single row (:issue:`35462`)
2525
- Fixed regression in ``.groupby(..).rolling(..)`` where a custom ``BaseIndexer`` would be ignored (:issue:`35557`)
26+
- Fixed regression in :meth:`DataFrame.replace` and :meth:`Series.replace` where compiled regular expressions would be ignored during replacement (:issue:`35680`)
2627

2728
.. ---------------------------------------------------------------------------
2829

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1949,7 +1949,7 @@ def _check_comparison_types(
19491949
else:
19501950
op = np.vectorize(
19511951
lambda x: bool(re.search(b, x))
1952-
if isinstance(x, str) and isinstance(b, str)
1952+
if isinstance(x, str) and isinstance(b, (str, re.Pattern))
19531953
else False
19541954
)
19551955

pandas/tests/frame/methods/test_replace.py

+8
Original file line numberDiff line numberDiff line change
@@ -1573,3 +1573,11 @@ def test_replace_dict_category_type(self, input_category_df, expected_category_d
15731573
result = input_df.replace({"a": "z", "obj1": "obj9", "cat1": "catX"})
15741574

15751575
tm.assert_frame_equal(result, expected)
1576+
1577+
def test_replace_with_compiled_regex(self):
1578+
# https://github.com/pandas-dev/pandas/issues/35680
1579+
df = pd.DataFrame(["a", "b", "c"])
1580+
regex = re.compile("^a$")
1581+
result = df.replace({regex: "z"}, regex=True)
1582+
expected = pd.DataFrame(["z", "b", "c"])
1583+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_replace.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -415,3 +417,11 @@ def test_replace_extension_other(self):
415417
# https://github.com/pandas-dev/pandas/issues/34530
416418
ser = pd.Series(pd.array([1, 2, 3], dtype="Int64"))
417419
ser.replace("", "") # no exception
420+
421+
def test_replace_with_compiled_regex(self):
422+
# https://github.com/pandas-dev/pandas/issues/35680
423+
s = pd.Series(["a", "b", "c"])
424+
regex = re.compile("^a$")
425+
result = s.replace({regex: "z"}, regex=True)
426+
expected = pd.Series(["z", "b", "c"])
427+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)