Skip to content

BUG: fixes #53935 Categorical order lost after call to remove_categories #54027

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

Merged
merged 11 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

- Bug in :meth:`Series.astype` with ``dtype="category"`` for nullable arrays with read-only null value masks (:issue:`53658`)
- Bug in :meth:`Series.map` , where the value of the ``na_action`` parameter was not used if the series held a :class:`Categorical` (:issue:`22527`).
-
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3572,7 +3572,7 @@ def _intersection_via_get_indexer(
return result

@final
def difference(self, other, sort=None):
def difference(self, other, sort=False):
"""
Return a new Index with elements of index not in `other`.

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,18 @@ def test_method_delegation(self):
msg = "cannot use inplace with CategoricalIndex"
with pytest.raises(ValueError, match=msg):
ci.set_categories(list("cab"), inplace=True)

def test_remove_maintains_order(self):
ci = CategoricalIndex(list("abcdda"), categories=list("abcd"))
result = ci.reorder_categories(["d", "c", "b", "a"], ordered=True)
tm.assert_index_equal(
result,
CategoricalIndex(list("abcdda"), categories=list("dcba"), ordered=True),
)
result = result.remove_categories(["c"])
tm.assert_index_equal(
result,
CategoricalIndex(
["a", "b", np.nan, "d", "d", "a"], categories=list("dba"), ordered=True
),
)