Skip to content

Commit 9f95f7d

Browse files
topper-123jreback
authored andcommitted
Improve performance of CategoricalIndex.is_unique (pandas-dev#21107)
1 parent 686f604 commit 9f95f7d

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
@@ -32,6 +32,7 @@ Performance Improvements
3232
~~~~~~~~~~~~~~~~~~~~~~~~
3333

3434
- Improved performance of :meth:`CategoricalIndex.is_monotonic_increasing`, :meth:`CategoricalIndex.is_monotonic_decreasing` and :meth:`CategoricalIndex.is_monotonic` (:issue:`21025`)
35+
- Improved performance of :meth:`CategoricalIndex.is_unique` (:issue:`21107`)
3536
-
3637
-
3738

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)