Skip to content

Commit

Permalink
Improve performance of CategoricalIndex.is_unique (pandas-dev#21107)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9f95f7d)
  • Loading branch information
topper-123 authored and TomAugspurger committed Jun 12, 2018
1 parent 1b8c041 commit 21775b6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~

- Improved performance of :meth:`CategoricalIndex.is_monotonic_increasing`, :meth:`CategoricalIndex.is_monotonic_decreasing` and :meth:`CategoricalIndex.is_monotonic` (:issue:`21025`)
- Improved performance of :meth:`CategoricalIndex.is_unique` (:issue:`21107`)
-
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def _engine(self):
# introspection
@cache_readonly
def is_unique(self):
return not self.duplicated().any()
return self._engine.is_unique

@property
def is_monotonic_increasing(self):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,15 @@ def test_is_monotonic(self, data, non_lexsorted_data):
assert c.is_monotonic_increasing
assert not c.is_monotonic_decreasing

@pytest.mark.parametrize('values, expected', [
([1, 2, 3], True),
([1, 3, 1], False),
(list('abc'), True),
(list('aba'), False)])
def test_is_unique(self, values, expected):
ci = CategoricalIndex(values)
assert ci.is_unique is expected

def test_duplicates(self):

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

0 comments on commit 21775b6

Please sign in to comment.