From c05fbf3fe3a41fe36e8507f3620407795b0e95f0 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Wed, 2 Aug 2017 05:55:18 -0400 Subject: [PATCH 1/3] correctly determine bottleneck version --- pandas/core/nanops.py | 3 ++- pandas/tests/reshape/test_concat.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) 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/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 46fea86c45925..4c8ba5a0afaac 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1934,6 +1934,7 @@ 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]) From 2d04cb94878cab036348d81a21059347f95b542b Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Wed, 2 Aug 2017 06:15:01 -0400 Subject: [PATCH 2/3] tests for categorical index monotonicity --- pandas/core/indexes/category.py | 13 +++++++++++ pandas/tests/indexes/test_category.py | 32 +++++++++++++++++++++++++++ pandas/tests/reshape/test_concat.py | 1 - 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index e8427f847dd2d..7ee22c4d223f3 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -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 + + @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/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') diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 4c8ba5a0afaac..46fea86c45925 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -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]) From 30c6281dc5de14722a35f6c27079cc0f5c7ce88c Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Wed, 2 Aug 2017 06:54:08 -0400 Subject: [PATCH 3/3] fix Index.is_monotonic to point to Index.is_monotonic_increasing directly --- pandas/core/indexes/base.py | 2 +- pandas/core/indexes/category.py | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) 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 7ee22c4d223f3..ac4698b570d17 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -321,10 +321,6 @@ def _engine(self): def is_unique(self): return not self.duplicated().any() - @property - def is_monotonic(self): - return self.is_monotonic_increasing - @property def is_monotonic_increasing(self): return Index(self.codes).is_monotonic_increasing