Skip to content

Commit 103d192

Browse files
committed
BUG: Series(Categorical(...)).unique() should not change the categories order
1 parent efb1a1b commit 103d192

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

pandas/core/indexes/category.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,7 @@ def unique(self, level=None):
365365
if level not in {0, self.name, None}:
366366
raise ValueError("Level {} not found".format(level))
367367
result = base.IndexOpsMixin.unique(self)
368-
# CategoricalIndex._shallow_copy uses keeps original categories
369-
# and ordered if not otherwise specified
370-
return self._shallow_copy(result, categories=result.categories,
371-
ordered=result.ordered)
368+
return self._shallow_copy(result)
372369

373370
@Appender(base._shared_docs['duplicated'] % _index_doc_kwargs)
374371
def duplicated(self, keep='first'):

pandas/tests/test_categorical.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1708,15 +1708,17 @@ def test_unique_index_series(self):
17081708
# if ordered=False
17091709
exp = Categorical([3, 1, 2], categories=[3, 1, 2])
17101710
tm.assert_categorical_equal(c.unique(), exp)
1711+
tm.assert_categorical_equal(pd.Series(c).unique(), exp)
17111712

1713+
exp = Categorical([3, 1, 2], categories=[3, 2, 1])
17121714
tm.assert_index_equal(Index(c).unique(), Index(exp))
1713-
tm.assert_categorical_equal(pd.Series(c).unique(), exp)
17141715

17151716
c = Categorical([1, 1, 2, 2], categories=[3, 2, 1])
17161717
exp = Categorical([1, 2], categories=[1, 2])
17171718
tm.assert_categorical_equal(c.unique(), exp)
1718-
tm.assert_index_equal(Index(c).unique(), Index(exp))
17191719
tm.assert_categorical_equal(pd.Series(c).unique(), exp)
1720+
exp = Categorical([1, 2], categories=[3, 2, 1])
1721+
tm.assert_index_equal(Index(c).unique(), Index(exp))
17201722

17211723
c = Categorical([3, 1, 2, 2, 1], categories=[3, 2, 1], ordered=True)
17221724
# Categorical.unique keeps categories order if ordered=True

0 commit comments

Comments
 (0)