Skip to content

Commit 3fadc62

Browse files
authored
TST: test for categorical index monotonicity (#17152)
* correctly determine bottleneck version * tests for categorical index monotonicity * fix Index.is_monotonic to point to Index.is_monotonic_increasing directly
1 parent 8e6b09f commit 3fadc62

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

Diff for: pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ def _mpl_repr(self):
11951195
@property
11961196
def is_monotonic(self):
11971197
""" alias for is_monotonic_increasing (deprecated) """
1198-
return self._engine.is_monotonic_increasing
1198+
return self.is_monotonic_increasing
11991199

12001200
@property
12011201
def is_monotonic_increasing(self):

Diff for: pandas/core/indexes/category.py

+9
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,19 @@ def _engine(self):
316316
# we are going to look things up with the codes themselves
317317
return self._engine_type(lambda: self.codes.astype('i8'), len(self))
318318

319+
# introspection
319320
@cache_readonly
320321
def is_unique(self):
321322
return not self.duplicated().any()
322323

324+
@property
325+
def is_monotonic_increasing(self):
326+
return Index(self.codes).is_monotonic_increasing
327+
328+
@property
329+
def is_monotonic_decreasing(self):
330+
return Index(self.codes).is_monotonic_decreasing
331+
323332
@Appender(base._shared_docs['unique'] % _index_doc_kwargs)
324333
def unique(self):
325334
result = base.IndexOpsMixin.unique(self)

Diff for: pandas/core/nanops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
try:
2929
import bottleneck as bn
3030
ver = bn.__version__
31-
_BOTTLENCK_INSTALLED = ver >= LooseVersion(_MIN_BOTTLENECK_VERSION)
31+
_BOTTLENECK_INSTALLED = (LooseVersion(ver) >=
32+
LooseVersion(_MIN_BOTTLENECK_VERSION))
3233

3334
if not _BOTTLENECK_INSTALLED:
3435
warnings.warn(

Diff for: pandas/tests/indexes/test_category.py

+32
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,38 @@ def test_reindex_empty_index(self):
427427
tm.assert_numpy_array_equal(indexer,
428428
np.array([-1, -1], dtype=np.intp))
429429

430+
def test_is_monotonic(self):
431+
c = CategoricalIndex([1, 2, 3])
432+
assert c.is_monotonic_increasing
433+
assert not c.is_monotonic_decreasing
434+
435+
c = CategoricalIndex([1, 2, 3], ordered=True)
436+
assert c.is_monotonic_increasing
437+
assert not c.is_monotonic_decreasing
438+
439+
c = CategoricalIndex([1, 2, 3], categories=[3, 2, 1])
440+
assert not c.is_monotonic_increasing
441+
assert c.is_monotonic_decreasing
442+
443+
c = CategoricalIndex([1, 3, 2], categories=[3, 2, 1])
444+
assert not c.is_monotonic_increasing
445+
assert not c.is_monotonic_decreasing
446+
447+
c = CategoricalIndex([1, 2, 3], categories=[3, 2, 1], ordered=True)
448+
assert not c.is_monotonic_increasing
449+
assert c.is_monotonic_decreasing
450+
451+
# non lexsorted categories
452+
categories = [9, 0, 1, 2, 3]
453+
454+
c = CategoricalIndex([9, 0], categories=categories)
455+
assert c.is_monotonic_increasing
456+
assert not c.is_monotonic_decreasing
457+
458+
c = CategoricalIndex([0, 1], categories=categories)
459+
assert c.is_monotonic_increasing
460+
assert not c.is_monotonic_decreasing
461+
430462
def test_duplicates(self):
431463

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

0 commit comments

Comments
 (0)