-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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: fixes #53935 Categorical order lost after call to remove_categories #54027
Conversation
…d. This allows the difference method to then call the _difference method and finally call the _maybe_try_sort method. In the _maybe_try_sort_method it will sort the values if sort is not False. That's why in the original code haveing sort=None would still sort the categories. This way the code will only sort if you set sort=True.
…andas-paulreece into categorical_sort_fix
…s set to True, if so it sets sort=False in the call to difference in remove_categories.
@@ -1366,7 +1366,10 @@ def remove_categories(self, removals) -> Self: | |||
removals = [removals] | |||
|
|||
removals = Index(removals).unique().dropna() | |||
new_categories = self.dtype.categories.difference(removals) |
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.
Can you just replace the if/else
with difference(removals, sort=not self.dtype.ordered)
?
doc/source/whatsnew/v2.1.0.rst
Outdated
@@ -371,6 +371,7 @@ Bug fixes | |||
|
|||
Categorical | |||
^^^^^^^^^^^ | |||
- Bug in :meth:`CategoricalIndex.remove_categories` , with categories it now retains the original order when removing an element from the categories list(:issue:`53935`). |
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.
- Bug in :meth:`CategoricalIndex.remove_categories` , with categories it now retains the original order when removing an element from the categories list(:issue:`53935`). | |
- Bug in :meth:`CategoricalIndex.remove_categories` where ordered categories would not be maintained (:issue:`53935`). |
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.
Just pushed up a commit that implements both suggestions. Thanks for your help!
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.
Unfortunately, that implementation you suggested failed the CI tests. I believe because we were overriding the default argument of None
with True
some of the time. I just uploaded a version using a Python Ternary to get this to a one liner. Let me know what you think.
…s set to True, if so it sets sort=False in the call to difference in remove_categories.
…eems to work better since we are not overriding a default argument this way.
Thanks @paulreece |
Great working with you, @mroeschke! |
remove_categories
#53935I simply put an if/else in the
remove_categories
method. Ifordered=True
it will passsort=False
into the `difference1 method.