Skip to content

Commit 21775b6

Browse files
topper-123TomAugspurger
authored andcommitted
Improve performance of CategoricalIndex.is_unique (pandas-dev#21107)
(cherry picked from commit 9f95f7d)
1 parent 1b8c041 commit 21775b6

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v0.23.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Performance Improvements
3030
~~~~~~~~~~~~~~~~~~~~~~~~
3131

3232
- Improved performance of :meth:`CategoricalIndex.is_monotonic_increasing`, :meth:`CategoricalIndex.is_monotonic_decreasing` and :meth:`CategoricalIndex.is_monotonic` (:issue:`21025`)
33+
- Improved performance of :meth:`CategoricalIndex.is_unique` (:issue:`21107`)
3334
-
3435
-
3536

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def _engine(self):
378378
# introspection
379379
@cache_readonly
380380
def is_unique(self):
381-
return not self.duplicated().any()
381+
return self._engine.is_unique
382382

383383
@property
384384
def is_monotonic_increasing(self):

pandas/tests/indexes/test_category.py

+9
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,15 @@ def test_is_monotonic(self, data, non_lexsorted_data):
581581
assert c.is_monotonic_increasing
582582
assert not c.is_monotonic_decreasing
583583

584+
@pytest.mark.parametrize('values, expected', [
585+
([1, 2, 3], True),
586+
([1, 3, 1], False),
587+
(list('abc'), True),
588+
(list('aba'), False)])
589+
def test_is_unique(self, values, expected):
590+
ci = CategoricalIndex(values)
591+
assert ci.is_unique is expected
592+
584593
def test_duplicates(self):
585594

586595
idx = CategoricalIndex([0, 0, 0], name='foo')

0 commit comments

Comments
 (0)