-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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: Categorical comparison with unordered #16339
Changes from all commits
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 |
---|---|---|
|
@@ -55,17 +55,31 @@ def f(self, other): | |
"equality or not") | ||
if isinstance(other, Categorical): | ||
# Two Categoricals can only be be compared if the categories are | ||
# the same | ||
if ((len(self.categories) != len(other.categories)) or | ||
not ((self.categories == other.categories).all())): | ||
raise TypeError("Categoricals can only be compared if " | ||
"'categories' are the same") | ||
# the same (maybe up to ordering, depending on ordered) | ||
|
||
msg = ("Categoricals can only be compared if " | ||
"'categories' are the same.") | ||
if len(self.categories) != len(other.categories): | ||
raise TypeError(msg + " Categories are different lengths") | ||
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. I think you are including the origninal message here, but it its a little awkward 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. What did you have in mind? I like how it's currently "Categoricals can only be compared if 'categories' are the same. Categories are different lengths." Since it's the general problem (different categories) and a specific hint 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. actually this is fine. |
||
elif (self.ordered and not (self.categories == | ||
other.categories).all()): | ||
raise TypeError(msg) | ||
elif not set(self.categories) == set(other.categories): | ||
raise TypeError(msg) | ||
|
||
if not (self.ordered == other.ordered): | ||
raise TypeError("Categoricals can only be compared if " | ||
"'ordered' is the same") | ||
na_mask = (self._codes == -1) | (other._codes == -1) | ||
if not self.ordered and not self.categories.equals( | ||
other.categories): | ||
# both unordered and different order | ||
other_codes = _get_codes_for_values(other, self.categories) | ||
else: | ||
other_codes = other._codes | ||
|
||
na_mask = (self._codes == -1) | (other_codes == -1) | ||
f = getattr(self._codes, op) | ||
ret = f(other._codes) | ||
ret = f(other_codes) | ||
if na_mask.any(): | ||
# In other series, the leads to False, so do that here too | ||
ret[na_mask] = False | ||
|
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.
versionadded tag
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.
I think not necessary since it's a bugfix.