-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
Changes from 11 commits
41c1ee5
048ed82
1fa4935
6dc3604
de36ab6
690a5a1
65fcbb6
8ee0005
b19b2af
5fee01e
f565d8b
ec3df78
b56c163
93cf495
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
else: | ||
return super().replace(to_replace, value, inplace, regex) | ||
|
||
|
||
class ObjectValuesExtensionBlock(ExtensionBlock): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you parametrize these using frame_or_series There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change this to: