Skip to content

PERF: improved performance of CategoricalIndex.is_monotonic* #21025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions asv_bench/benchmarks/categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,23 @@ def setup(self, dtype):

def time_isin_categorical(self, dtype):
self.series.isin(self.sample)


class IsMonotonic(object):

def setup(self):
N = 1000
self.c = pd.CategoricalIndex(list('a' * N + 'b' * N + 'c' * N))
self.s = pd.Series(self.c)

def time_categorical_index_is_monotonic_increasing(self):
self.c.is_monotonic_increasing

def time_categorical_index_is_monotonic_decreasing(self):
self.c.is_monotonic_decreasing

def time_categorical_series_is_monotonic_increasing(self):
self.s.is_monotonic_increasing

def time_categorical_series_is_monotonic_decreasing(self):
self.s.is_monotonic_decreasing
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 @@ -29,6 +29,7 @@ Deprecations
Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~

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

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ def is_unique(self):

@property
def is_monotonic_increasing(self):
return Index(self.codes).is_monotonic_increasing
return self._engine.is_monotonic_increasing

@property
def is_monotonic_decreasing(self):
return Index(self.codes).is_monotonic_decreasing
return self._engine.is_monotonic_decreasing

@Appender(_index_shared_docs['index_unique'] % _index_doc_kwargs)
def unique(self, level=None):
Expand Down
28 changes: 17 additions & 11 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,35 +543,41 @@ def test_reindex_empty_index(self):
tm.assert_numpy_array_equal(indexer,
np.array([-1, -1], dtype=np.intp))

def test_is_monotonic(self):
c = CategoricalIndex([1, 2, 3])
@pytest.mark.parametrize('data, non_lexsorted_data', [
[[1, 2, 3], [9, 0, 1, 2, 3]],
[list('abc'), list('fabcd')],
])
def test_is_monotonic(self, data, non_lexsorted_data):
c = CategoricalIndex(data)
assert c.is_monotonic_increasing
assert not c.is_monotonic_decreasing

c = CategoricalIndex([1, 2, 3], ordered=True)
c = CategoricalIndex(data, ordered=True)
assert c.is_monotonic_increasing
assert not c.is_monotonic_decreasing

c = CategoricalIndex([1, 2, 3], categories=[3, 2, 1])
c = CategoricalIndex(data, categories=reversed(data))
assert not c.is_monotonic_increasing
assert c.is_monotonic_decreasing

c = CategoricalIndex([1, 3, 2], categories=[3, 2, 1])
c = CategoricalIndex(data, categories=reversed(data), ordered=True)
assert not c.is_monotonic_increasing
assert not c.is_monotonic_decreasing
assert c.is_monotonic_decreasing

c = CategoricalIndex([1, 2, 3], categories=[3, 2, 1], ordered=True)
# test when data is neither monotonic increasing nor decreasing
reordered_data = [data[0], data[2], data[1]]
c = CategoricalIndex(reordered_data, categories=reversed(data))
assert not c.is_monotonic_increasing
assert c.is_monotonic_decreasing
assert not c.is_monotonic_decreasing

# non lexsorted categories
categories = [9, 0, 1, 2, 3]
categories = non_lexsorted_data

c = CategoricalIndex([9, 0], categories=categories)
c = CategoricalIndex(categories[:2], categories=categories)
assert c.is_monotonic_increasing
assert not c.is_monotonic_decreasing

c = CategoricalIndex([0, 1], categories=categories)
c = CategoricalIndex(categories[1:3], categories=categories)
assert c.is_monotonic_increasing
assert not c.is_monotonic_decreasing

Expand Down