Skip to content
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

Merged
merged 11 commits into from
Jul 11, 2023

Conversation

paulreece
Copy link
Contributor

@paulreece paulreece commented Jul 6, 2023

I simply put an if/else in the remove_categories method. If ordered=True it will pass sort=False into the `difference1 method.

…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.
@paulreece paulreece changed the title Changed the default value for sort to 'False' in the difference method BUG: Changed the default value for sort to 'False' in the difference method Jul 6, 2023
@paulreece paulreece changed the title BUG: Changed the default value for sort to 'False' in the difference method BUG: Categorical order lost after call to remove_categories #53935 Jul 6, 2023
@paulreece paulreece changed the title BUG: Categorical order lost after call to remove_categories #53935 BUG: Fixes #53935 Categorical order lost after call to remove_categories Jul 6, 2023
@paulreece paulreece changed the title BUG: Fixes #53935 Categorical order lost after call to remove_categories BUG: fixes #53935 Categorical order lost after call to remove_categories Jul 6, 2023
pandas/core/indexes/base.py Outdated Show resolved Hide resolved
@mroeschke mroeschke added the Categorical Categorical Data Type label Jul 7, 2023
paulreece and others added 2 commits July 7, 2023 17:34
…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)
Copy link
Member

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)?

@@ -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`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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`).

Copy link
Contributor Author

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!

Copy link
Contributor Author

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.

paulreece and others added 4 commits July 10, 2023 17:52
…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.
@mroeschke mroeschke added this to the 2.1 milestone Jul 11, 2023
@mroeschke mroeschke merged commit 41dfccc into pandas-dev:main Jul 11, 2023
@mroeschke
Copy link
Member

Thanks @paulreece

@paulreece
Copy link
Contributor Author

Great working with you, @mroeschke!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants