Skip to content

BUG: 35977 Adding regex support for ExtensionBlock replace method #36038

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,11 @@ Sparse
ExtensionArray
^^^^^^^^^^^^^^


- Fixed Bug where :class:`DataFrame` column set to scalar extension type via a dict instantion was considered an object type rather than the extension type (:issue:`35965`)
- Fixed bug where ``astype()`` with equal dtype and ``copy=False`` would return a new object (:issue:`284881`)
- Fixed bug when applying a NumPy ufunc with multiple outputs to a :class:`pandas.arrays.IntegerArray` returning None (:issue:`36913`)
- Fixed bug in :meth:`Dataframe.replace` not working for ``regex=True`` and ``dtype='string'`` Now working as expected (:issue:`35977`)
Copy link
Member

Choose a reason for hiding this comment

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

Please change this to:

- Fixed bug in :meth:`DataFrame.replace` now returns correct result for `regex=True`` with ``string`` dtype (:issue:`35977`)



Other
Expand Down
27 changes: 27 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,33 @@ def _unstack(self, unstacker, fill_value, new_placement):
]
return blocks, mask

def replace(
self,
to_replace,
value,
inplace: bool = False,
regex: bool = False,
convert: bool = True,
):
"""
replace the to_replace value with value, regex is not supported by super class
when regex is required ObjectBlock replace method is called
"""
inplace = validate_bool_kwarg(inplace, "inplace")
regex = validate_bool_kwarg(regex, "regex")
if regex:
dtype = self.values.dtype
blocks = self.astype(object)
Copy link
Member

Choose a reason for hiding this comment

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

can you give this a non-plural name

Copy link
Member

Choose a reason for hiding this comment

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

@cgangwar11 can you address?

if not inplace:
return [
b.astype(dtype)
for b in blocks.replace(to_replace, value, inplace, regex, convert)
]
blocks.replace(to_replace, value, inplace, regex, convert)
return blocks.astype(dtype)
else:
return super().replace(to_replace, value, inplace, regex, convert)


class ObjectValuesExtensionBlock(ExtensionBlock):
"""
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/arrays/categorical/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,41 @@ def test_replace(to_replace, value, expected, flip_categories):

tm.assert_series_equal(expected, result, check_category_order=False)
tm.assert_series_equal(expected, s, check_category_order=False)


@pytest.mark.parametrize(
"to_replace,value,input_data,expected_data,inplace",
[
(r"^\s*$", pd.NA, ["d", "ee", "f", ""], ["d", "ee", "f", pd.NA], False),
(r"e{2}", "replace", ["d", "ee", "f", ""], ["d", "replace", "f", ""], False),
(r"f", "replace", ["d", "ee", "f", ""], ["d", "ee", "replace", ""], False),
(r"^\s*$", pd.NA, ["d", "ee", "f", ""], ["d", "ee", "f", pd.NA], True),
(r"e{2}", "replace", ["d", "ee", "f", ""], ["d", "replace", "f", ""], True),
(r"f", "replace", ["d", "ee", "f", ""], ["d", "ee", "replace", ""], True),
],
)
def test_replace_regex(to_replace, value, input_data, expected_data, inplace):
Copy link
Member

Choose a reason for hiding this comment

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

if this is testing DataFrame.replace, these belong in tests.frame.methods.test_replace

# GH35977
Copy link
Member

Choose a reason for hiding this comment

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

Use the idiom:

result = ...
expected = ...
tm.assert_frame_equal(result, frame)

input_df = pd.DataFrame({"col1": input_data}, dtype="string")
Copy link
Member

Choose a reason for hiding this comment

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

Call this df

expected_df = pd.DataFrame({"col1": expected_data}, dtype="string")
Copy link
Member

Choose a reason for hiding this comment

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

Call this expected

converted = input_df.replace(to_replace, value, inplace=inplace, regex=True)
Copy link
Member

Choose a reason for hiding this comment

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

Call this df_replaced

if inplace:
Copy link
Member

Choose a reason for hiding this comment

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

do

result = df_replaced if inplace else df

tm.assert_frame_equal(expected_df, input_df)
else:
tm.assert_frame_equal(expected_df, converted)


@pytest.mark.parametrize(
"to_replace,value,input_data,expected_data",
[
("", pd.NA, ["d", "ee", "f", ""], ["d", "ee", "f", pd.NA]),
("ee", "replace", ["d", "ee", "f", ""], ["d", "replace", "f", ""]),
("f", "replace", ["d", "ee", "f", ""], ["d", "ee", "replace", ""]),
],
)
def test_replace_string(to_replace, value, input_data, expected_data):
Copy link
Member

Choose a reason for hiding this comment

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

Use the idiom:

result = ...
expected = ...
tm.assert_frame_equal(result, frame)

# GH35977
input_df = pd.DataFrame({"col1": input_data}, dtype="string")
Copy link
Member

Choose a reason for hiding this comment

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

Call this df

expected_df = pd.DataFrame({"col1": expected_data}, dtype="string")
Copy link
Member

Choose a reason for hiding this comment

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

Call this expected

input_df = input_df.replace(to_replace, value, inplace=False, regex=False)
Copy link
Member

Choose a reason for hiding this comment

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

Call this result

tm.assert_frame_equal(expected_df, input_df)