Skip to content

Commit 7cc1a45

Browse files
author
tp
committed
Improve performance of CategoricalIndex.is_unique
1 parent d623ffd commit 7cc1a45

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-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

+10
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,16 @@ 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('unique, not_unique',
585+
[([1, 2, 3], [1, 3, 2]),
586+
(list('abc'), list('acb')),
587+
])
588+
def test_is_unique(self, unique, not_unique):
589+
ci = CategoricalIndex(unique)
590+
assert ci.is_unique
591+
ci = CategoricalIndex(not_unique)
592+
assert ci.is_unique
593+
584594
def test_duplicates(self):
585595

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

0 commit comments

Comments
 (0)