Skip to content

REGR: replace with multivalued regex raising #40604

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 7 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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/v1.2.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.sum` when ``min_count`` greater than the :class:`DataFrame` shape was passed resulted in a ``ValueError`` (:issue:`39738`)
- Fixed regression in :meth:`DataFrame.to_json` raising ``AttributeError`` when run on PyPy (:issue:`39837`)
- Fixed regression in :meth:`DataFrame.where` not returning a copy in the case of an all True condition (:issue:`39595`)
- Fixed regression in :meth:`DataFrame.replace` raising ``IndexError`` when ``regex`` was a multi-key dictionary (:issue:`39338`)
-

.. ---------------------------------------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,23 @@ def _replace_list(

rb = [self if inplace else self.copy()]
for i, (src, dest) in enumerate(pairs):
convert = i == src_len # only convert once at the end
new_rb: List[Block] = []
mask_pos = 0
for blk in rb:
m = masks[i]
convert = i == src_len # only convert once at the end
if blk.ndim == 1:
m = masks[i]
else:
# GH-39338: _replace_coerce can split a block, so we
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it gets split, will it be all-single-column?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question will look into that, would help simplify logic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted to account for this - split is happening in split_and_operate, which looks to always give single column result

# need to keep track of where to index into the mask
assert not isinstance(masks[i], bool)
# error: Value of type "Union[ExtensionArray, ndarray, bool]"
# is not indexable
m = masks[i][
mask_pos : mask_pos + blk.shape[0]
] # type: ignore[index]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think if you define mib = masks[i] or something that might make this type:ignore unnecessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call!

mask_pos += blk.shape[0]

result = blk._replace_coerce(
to_replace=src,
value=dest,
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,16 @@ def test_regex_replace_numeric_to_object_conversion(self, mix_abc):
tm.assert_frame_equal(res, expec)
assert res.a.dtype == np.object_

@pytest.mark.parametrize(
"to_replace", [{"": np.nan, ",": ""}, {",": "", "": np.nan}]
)
def test_joint_simple_replace_and_regex_replace(self, to_replace):
# GH-39338
df = DataFrame({"col1": ["1,000", "a", "3"], "col2": ["a", "", "b"]})
result = df.replace(regex=to_replace)
expected = DataFrame({"col1": ["1000", "a", "3"], "col2": ["a", np.nan, "b"]})
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("metachar", ["[]", "()", r"\d", r"\w", r"\s"])
def test_replace_regex_metachar(self, metachar):
df = DataFrame({"a": [metachar, "else"]})
Expand Down