diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 411428e001c81..4aecc75d95971 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1195,7 +1195,7 @@ def _mpl_repr(self): @property def is_monotonic(self): """ alias for is_monotonic_increasing (deprecated) """ - return self._engine.is_monotonic_increasing + return self.is_monotonic_increasing @property def is_monotonic_increasing(self): diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index e8427f847dd2d..ac4698b570d17 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -316,10 +316,19 @@ def _engine(self): # we are going to look things up with the codes themselves return self._engine_type(lambda: self.codes.astype('i8'), len(self)) + # introspection @cache_readonly def is_unique(self): return not self.duplicated().any() + @property + def is_monotonic_increasing(self): + return Index(self.codes).is_monotonic_increasing + + @property + def is_monotonic_decreasing(self): + return Index(self.codes).is_monotonic_decreasing + @Appender(base._shared_docs['unique'] % _index_doc_kwargs) def unique(self): result = base.IndexOpsMixin.unique(self) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index e2777cb56374e..2f4e437c0ae61 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -28,7 +28,8 @@ try: import bottleneck as bn ver = bn.__version__ - _BOTTLENCK_INSTALLED = ver >= LooseVersion(_MIN_BOTTLENECK_VERSION) + _BOTTLENECK_INSTALLED = (LooseVersion(ver) >= + LooseVersion(_MIN_BOTTLENECK_VERSION)) if not _BOTTLENECK_INSTALLED: warnings.warn( diff --git a/pandas/tests/indexes/test_category.py b/pandas/tests/indexes/test_category.py index a3d72fdb88239..64bd6df361aeb 100644 --- a/pandas/tests/indexes/test_category.py +++ b/pandas/tests/indexes/test_category.py @@ -427,6 +427,38 @@ 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]) + assert c.is_monotonic_increasing + assert not c.is_monotonic_decreasing + + c = CategoricalIndex([1, 2, 3], ordered=True) + assert c.is_monotonic_increasing + assert not c.is_monotonic_decreasing + + c = CategoricalIndex([1, 2, 3], categories=[3, 2, 1]) + assert not c.is_monotonic_increasing + assert c.is_monotonic_decreasing + + c = CategoricalIndex([1, 3, 2], categories=[3, 2, 1]) + assert not c.is_monotonic_increasing + assert not c.is_monotonic_decreasing + + c = CategoricalIndex([1, 2, 3], categories=[3, 2, 1], ordered=True) + assert not c.is_monotonic_increasing + assert c.is_monotonic_decreasing + + # non lexsorted categories + categories = [9, 0, 1, 2, 3] + + c = CategoricalIndex([9, 0], categories=categories) + assert c.is_monotonic_increasing + assert not c.is_monotonic_decreasing + + c = CategoricalIndex([0, 1], categories=categories) + assert c.is_monotonic_increasing + assert not c.is_monotonic_decreasing + def test_duplicates(self): idx = CategoricalIndex([0, 0, 0], name='foo')