Skip to content
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

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 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 @@ -567,9 +567,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`)

- Fixed an inconsistency in :class:`PeriodArray`'s ``__init__`` signature to those of :class:`DatetimeArray` and :class:`TimedeltaArray` (:issue:`37289`)
- Reductions for :class:`BooleanArray`, :class:`Categorical`, :class:`DatetimeArray`, :class:`FloatingArray`, :class:`IntegerArray`, :class:`PeriodArray`, :class:`TimedeltaArray`, and :class:`PandasArray` are now keyword-only methods (:issue:`37541`)

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

def replace(
self,
to_replace,
value,
inplace: bool = False,
regex: bool = False
):
"""
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
block = self.astype(object)
if not inplace:
return [
b.astype(dtype)
for b in block.replace(to_replace, value, inplace, regex)
]
block.replace(to_replace, value, inplace, regex)
return block.astype(dtype)
Copy link
Member

Choose a reason for hiding this comment

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

We recently implement Block._replace_regex. can you see if we can avoid the object-dtype cast (ideally even this whole method) by using that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

replace_regex is using np.vectorize function internally which gives an error when using not object dtype hence using replace_regex after typecasting.
f = np.vectorize(re_replacer, otypes=[values.dtype])

else:
return super().replace(to_replace, value, inplace, regex)


class ObjectValuesExtensionBlock(ExtensionBlock):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ def test_replace2(to_replace, value, result, expected_error_msg):
# ensure non-inplace call does not affect original
tm.assert_categorical_equal(cat, expected)
cat.replace(to_replace, value, inplace=True)
tm.assert_categorical_equal(cat, expected)
tm.assert_categorical_equal(cat, expected)
34 changes: 34 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,3 +1632,37 @@ def test_replace_unicode(self):
result = df1.replace(columns_values_map)
expected = DataFrame({"positive": np.ones(3)})
tm.assert_frame_equal(result, expected)

@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(self, to_replace, value, input_data, expected_data, inplace):
# GH35977
df = pd.DataFrame({"col1": input_data}, dtype="string")
expected = pd.DataFrame({"col1": expected_data}, dtype="string")
df_replaced = df.replace(to_replace, value, inplace=inplace, regex=True)
result = df if inplace else df_replaced
tm.assert_frame_equal(result, expected)

@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(self, 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.

can you parametrize these using frame_or_series

Copy link
Member

Choose a reason for hiding this comment

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

was this addressed?

# GH35977
df = pd.DataFrame({"col1": input_data}, dtype="string")
expected = pd.DataFrame({"col1": expected_data}, dtype="string")
result = df.replace(to_replace, value, inplace=False, regex=False)
tm.assert_frame_equal(result, expected)