-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG-26988 implement replace for categorical blocks #27026
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 2 commits
770ad53
a2f2a7c
535ab5d
9482c2b
702e71d
da778e1
bec92d0
593e1e0
5f23dc9
74294e0
815fe50
f444347
44631bc
8667b0d
988ebc3
d8a8d1f
0154bba
c4d3f84
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 |
---|---|---|
|
@@ -2978,6 +2978,29 @@ def where(self, other, cond, align=True, errors='raise', | |
axis=axis, transpose=transpose) | ||
return result | ||
|
||
def replace(self, to_replace, value, inplace=False, filter=None, | ||
regex=False, convert=True): | ||
if filter is None or not self.mgr_locs.isin(filter).any(): | ||
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. this block looks unrelated to the linked issue. what case is it solving? needs a separate test? 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. The first case is used when the replace function applies to the entire block, so the replace can be carried out just by manipulating categories. Most calls to I added a test in |
||
result = self if inplace else self.copy() | ||
categories = result.values.categories.tolist() | ||
if to_replace in categories: | ||
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 we not define a Categorical.replace to handle this code here (from 3013 thru 3020) 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. done, with tests added in |
||
if isna(value): | ||
result.values.remove_categories(to_replace, inplace=True) | ||
else: | ||
index = categories.index(to_replace) | ||
categories[index] = value | ||
result.values.rename_categories(categories, inplace=True) | ||
if convert: | ||
return result.convert(by_item=True, numeric=False, | ||
copy=not inplace) | ||
else: | ||
return result | ||
else: | ||
if not isna(value): | ||
self.values.add_categories(value, inplace=True) | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return super().replace(to_replace, value, inplace, | ||
filter, regex, convert) | ||
|
||
|
||
# ----------------------------------------------------------------- | ||
# Constructor Helpers | ||
|
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.
"unusual" --> "incorrect"
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.
fixed