Skip to content

Commit

Permalink
TST: test for categorical index monotonicity (pandas-dev#17152)
Browse files Browse the repository at this point in the history
* correctly determine bottleneck version

* tests for categorical index monotonicity

* fix Index.is_monotonic to point to Index.is_monotonic_increasing directly
  • Loading branch information
jreback authored and jowens committed Sep 20, 2017
1 parent 9aadb64 commit 89fa421
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 89fa421

Please sign in to comment.