Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests for categorical index monotonicity
  • Loading branch information
jreback committed Aug 2, 2017
commit 2d04cb94878cab036348d81a21059347f95b542b
13 changes: 13 additions & 0 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,23 @@ 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(self):
return self.is_monotonic_increasing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it needed to add this one? Isn't this in the base class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in base class instead.


@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
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
1 change: 0 additions & 1 deletion pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,6 @@ def test_concat_categoricalindex(self):
c = pd.Series(3, index=pd.CategoricalIndex([1, 2],
categories=categories))

import pdb; pdb.set_trace()
result = pd.concat([a, b, c], axis=1)

exp_idx = pd.CategoricalIndex([0, 1, 2, 9])
Expand Down