-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 9 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 |
---|---|---|
|
@@ -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) | ||
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 give this a non-plural name 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. @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): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
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. if this is testing DataFrame.replace, these belong in tests.frame.methods.test_replace |
||
# GH35977 | ||
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. Use the idiom:
|
||
input_df = pd.DataFrame({"col1": input_data}, dtype="string") | ||
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. Call this |
||
expected_df = pd.DataFrame({"col1": expected_data}, dtype="string") | ||
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. Call this |
||
converted = input_df.replace(to_replace, value, inplace=inplace, regex=True) | ||
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. Call this |
||
if inplace: | ||
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. do
|
||
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): | ||
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. Use the idiom:
|
||
# GH35977 | ||
input_df = pd.DataFrame({"col1": input_data}, dtype="string") | ||
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. Call this |
||
expected_df = pd.DataFrame({"col1": expected_data}, dtype="string") | ||
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. Call this |
||
input_df = input_df.replace(to_replace, value, inplace=False, regex=False) | ||
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. Call this |
||
tm.assert_frame_equal(expected_df, input_df) |
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: