Skip to content

Backport PR #25266 on branch 0.24.x (BUG: Fix regression on DataFrame.replace for regex) #25477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ Fixed Regressions
- Fixed regression in :meth:`DataFrame.all` and :meth:`DataFrame.any` where ``bool_only=True`` was ignored (:issue:`25101`)
- Fixed issue in ``DataFrame`` construction with passing a mixed list of mixed types could segfault. (:issue:`25075`)
- Fixed regression in :meth:`DataFrame.apply` causing ``RecursionError`` when ``dict``-like classes were passed as argument. (:issue:`25196`)
- Fixed regression in :meth:`DataFrame.replace` where ``regex=True`` was only replacing patterns matching the start of the string (:issue:`25259`)

- Fixed regression in :meth:`DataFrame.duplicated()`, where empty dataframe was not returning a boolean dtyped Series. (:issue:`25184`)
- Fixed regression in :meth:`Series.min` and :meth:`Series.max` where ``numeric_only=True`` was ignored when the ``Series`` contained ```Categorical`` data (:issue:`25299`)
12 changes: 6 additions & 6 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
@@ -552,9 +552,9 @@ def comp(s, regex=False):
if isna(s):
return isna(values)
if hasattr(s, 'asm8'):
return _compare_or_regex_match(maybe_convert_objects(values),
getattr(s, 'asm8'), regex)
return _compare_or_regex_match(values, s, regex)
return _compare_or_regex_search(maybe_convert_objects(values),
getattr(s, 'asm8'), regex)
return _compare_or_regex_search(values, s, regex)

masks = [comp(s, regex) for i, s in enumerate(src_list)]

@@ -1901,11 +1901,11 @@ def _consolidate(blocks):
return new_blocks


def _compare_or_regex_match(a, b, regex=False):
def _compare_or_regex_search(a, b, regex=False):
"""
Compare two array_like inputs of the same shape or two scalar values

Calls operator.eq or re.match, depending on regex argument. If regex is
Calls operator.eq or re.search, depending on regex argument. If regex is
True, perform an element-wise regex matching.

Parameters
@@ -1921,7 +1921,7 @@ def _compare_or_regex_match(a, b, regex=False):
if not regex:
op = lambda x: operator.eq(x, b)
else:
op = np.vectorize(lambda x: bool(re.match(b, x)) if isinstance(x, str)
op = np.vectorize(lambda x: bool(re.search(b, x)) if isinstance(x, str)
else False)

is_a_array = isinstance(a, np.ndarray)
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_replace.py
Original file line number Diff line number Diff line change
@@ -466,6 +466,13 @@ def test_regex_replace_dict_nested(self):
assert_frame_equal(res3, expec)
assert_frame_equal(res4, expec)

def test_regex_replace_dict_nested_non_first_character(self):
# GH 25259
df = pd.DataFrame({'first': ['abc', 'bca', 'cab']})
expected = pd.DataFrame({'first': ['.bc', 'bc.', 'c.b']})
result = df.replace({'a': '.'}, regex=True)
assert_frame_equal(result, expected)

def test_regex_replace_dict_nested_gh4115(self):
df = pd.DataFrame({'Type': ['Q', 'T', 'Q', 'Q', 'T'], 'tmp': 2})
expected = DataFrame({'Type': [0, 1, 0, 0, 1], 'tmp': 2})