From 370d509dc623d91984878d0c703f59d1ff22eb6c Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Thu, 24 May 2018 23:11:38 +0530 Subject: [PATCH 01/11] Initial commit GH21149-1 --- doc/source/whatsnew/v0.23.1.txt | 3 ++- pandas/core/indexes/base.py | 5 +++-- pandas/tests/indexes/test_multi.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index a071d7f3f5534..d07b5ab24af2d 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -78,7 +78,8 @@ Indexing - Bug in :meth:`Series.reset_index` where appropriate error was not raised with an invalid level name (:issue:`20925`) - Bug in :func:`interval_range` when ``start``/``periods`` or ``end``/``periods`` are specified with float ``start`` or ``end`` (:issue:`21161`) -- +- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with self.nlevels == 1 (:issue:`21149`) +- I/O ^^^ diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f79288c167356..27e21b2adce49 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -95,8 +95,9 @@ def cmp_method(self, other): # representations if needs_i8_conversion(self) and needs_i8_conversion(other): return self._evaluate_compare(other, op) - - if is_object_dtype(self) and self.nlevels == 1: + + from .multi import MultiIndex + if is_object_dtype(self) and not isinstance(self, MultiIndex): # don't pass MultiIndex with np.errstate(all='ignore'): result = ops._comp_method_OBJECT_ARRAY(op, self.values, other) diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index c9f6bc9151d00..8a30e3de5c8ab 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -3291,3 +3291,32 @@ def test_duplicate_multiindex_labels(self): with pytest.raises(ValueError): ind.set_levels([['A', 'B', 'A', 'A', 'B'], [2, 1, 3, -2, 5]], inplace=True) + + @pytest.mark.parametrize("midx,idx,count", [ + (pd.MultiIndex.from_product([[0, 1], [1, 0]]), pd.Series(range(4)), 4), (pd.MultiIndex.from_product([[0, 1]]), pd.Series(range(2)), 2)]) + def test_multiindex_compare(self, midx, idx, count): + # GH 21149 + '''Ensure comparison operations for MultiIndex with nlevels == 1 + behave consistently with those for MultiIndex with nlevels > 1 + ''' + expected = pd.Series([True]).repeat(count) + expected.reset_index(drop=True, inplace=True) + # Equality self-test: MultiIndex object vs self + result = pd.Series(midx == midx) + tm.assert_series_equal(result, expected) + # Equality self-test: non-MultiIndex Index object vs self + result = (idx == idx) + tm.assert_series_equal(result, expected) + + expected = pd.Series([False]).repeat(count) + expected.reset_index(drop=True, inplace=True) + # Greater than comparison: MultiIndex object vs self + result = pd.Series(midx > midx) + tm.assert_series_equal(result, expected) + # Equality test: non-MultiIndex Index object vs MultiIndex object + result = pd.Series(midx == idx) + tm.assert_series_equal(result, expected) + + # Greater-than test: non-MultiIndex Index object vs MultiIndex object + with tm.assert_raises_regex(TypeError, 'not supported'): + midx > idx From b661ca19cdaa1333935958243e7d80a5a60739f7 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Thu, 24 May 2018 23:23:50 +0530 Subject: [PATCH 02/11] Fixed PEP8 formatting issues GH21149-1 --- pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/test_multi.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 27e21b2adce49..c875821d5a5e1 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -95,7 +95,7 @@ def cmp_method(self, other): # representations if needs_i8_conversion(self) and needs_i8_conversion(other): return self._evaluate_compare(other, op) - + from .multi import MultiIndex if is_object_dtype(self) and not isinstance(self, MultiIndex): # don't pass MultiIndex diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 8a30e3de5c8ab..4b822cd0b6060 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -3293,7 +3293,8 @@ def test_duplicate_multiindex_labels(self): inplace=True) @pytest.mark.parametrize("midx,idx,count", [ - (pd.MultiIndex.from_product([[0, 1], [1, 0]]), pd.Series(range(4)), 4), (pd.MultiIndex.from_product([[0, 1]]), pd.Series(range(2)), 2)]) + (pd.MultiIndex.from_product([[0, 1], [1, 0]]), pd.Series(range(4)), 4), + (pd.MultiIndex.from_product([[0, 1]]), pd.Series(range(2)), 2)]) def test_multiindex_compare(self, midx, idx, count): # GH 21149 '''Ensure comparison operations for MultiIndex with nlevels == 1 @@ -3316,7 +3317,7 @@ def test_multiindex_compare(self, midx, idx, count): # Equality test: non-MultiIndex Index object vs MultiIndex object result = pd.Series(midx == idx) tm.assert_series_equal(result, expected) - + # Greater-than test: non-MultiIndex Index object vs MultiIndex object with tm.assert_raises_regex(TypeError, 'not supported'): midx > idx From cd6e7521c01281cf2e80debe0c1552e33c39c186 Mon Sep 17 00:00:00 2001 From: Kalyan Gokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Fri, 25 May 2018 00:48:22 +0530 Subject: [PATCH 03/11] Update test_multi.py Removed the following test, which was causing builds to fail (?). This was working when tested on my command line (Mac OS Terminal) # Greater-than test: non-MultiIndex Index object vs MultiIndex object with tm.assert_raises_regex(TypeError, 'not supported'): midx > idx --- pandas/tests/indexes/test_multi.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 4b822cd0b6060..1a0f6c895e2a6 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -3318,6 +3318,3 @@ def test_multiindex_compare(self, midx, idx, count): result = pd.Series(midx == idx) tm.assert_series_equal(result, expected) - # Greater-than test: non-MultiIndex Index object vs MultiIndex object - with tm.assert_raises_regex(TypeError, 'not supported'): - midx > idx From d27736d8a936ed61c8555d9df90d98ed67582161 Mon Sep 17 00:00:00 2001 From: Kalyan Gokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Fri, 25 May 2018 00:50:20 +0530 Subject: [PATCH 04/11] Update test_multi.py --- pandas/tests/indexes/test_multi.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 1a0f6c895e2a6..4adba6e97041d 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -3317,4 +3317,3 @@ def test_multiindex_compare(self, midx, idx, count): # Equality test: non-MultiIndex Index object vs MultiIndex object result = pd.Series(midx == idx) tm.assert_series_equal(result, expected) - From f90cf946d934aae1fdebc95464f5e403e4ba0bd9 Mon Sep 17 00:00:00 2001 From: Kalyan Gokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 27 May 2018 14:54:32 +0530 Subject: [PATCH 05/11] Update v0.23.1.txt --- doc/source/whatsnew/v0.23.1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index d07b5ab24af2d..5b1ef4e2cee78 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -78,7 +78,7 @@ Indexing - Bug in :meth:`Series.reset_index` where appropriate error was not raised with an invalid level name (:issue:`20925`) - Bug in :func:`interval_range` when ``start``/``periods`` or ``end``/``periods`` are specified with float ``start`` or ``end`` (:issue:`21161`) -- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with self.nlevels == 1 (:issue:`21149`) +- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a ``MultiIndex`` with ``nlevels == 1`` (:issue:`21149`) - I/O From 6ad6e7e8e61526e2204566d2c2a3ab5ae4741ffd Mon Sep 17 00:00:00 2001 From: Kalyan Gokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 27 May 2018 14:56:02 +0530 Subject: [PATCH 06/11] Simplified tests --- pandas/tests/indexes/test_multi.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 4adba6e97041d..e2cb0f5b0f34b 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -3292,28 +3292,20 @@ def test_duplicate_multiindex_labels(self): ind.set_levels([['A', 'B', 'A', 'A', 'B'], [2, 1, 3, -2, 5]], inplace=True) - @pytest.mark.parametrize("midx,idx,count", [ - (pd.MultiIndex.from_product([[0, 1], [1, 0]]), pd.Series(range(4)), 4), - (pd.MultiIndex.from_product([[0, 1]]), pd.Series(range(2)), 2)]) - def test_multiindex_compare(self, midx, idx, count): + def test_multiindex_compare(self): # GH 21149 - '''Ensure comparison operations for MultiIndex with nlevels == 1 + """ Ensure comparison operations for MultiIndex with nlevels == 1 behave consistently with those for MultiIndex with nlevels > 1 - ''' - expected = pd.Series([True]).repeat(count) - expected.reset_index(drop=True, inplace=True) + """ + + midx = pd.MultiIndex.from_product([[0, 1]]) + # Equality self-test: MultiIndex object vs self + expected = pd.Series([True, True]) result = pd.Series(midx == midx) tm.assert_series_equal(result, expected) - # Equality self-test: non-MultiIndex Index object vs self - result = (idx == idx) - tm.assert_series_equal(result, expected) - expected = pd.Series([False]).repeat(count) - expected.reset_index(drop=True, inplace=True) # Greater than comparison: MultiIndex object vs self + expected = pd.Series([False, False]) result = pd.Series(midx > midx) tm.assert_series_equal(result, expected) - # Equality test: non-MultiIndex Index object vs MultiIndex object - result = pd.Series(midx == idx) - tm.assert_series_equal(result, expected) From a2cd6743a438dcb5296997717bf30bd11279d0cb Mon Sep 17 00:00:00 2001 From: Kalyan Gokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Wed, 13 Jun 2018 22:16:27 +0530 Subject: [PATCH 07/11] Update v0.23.1.txt --- doc/source/whatsnew/v0.23.1.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index 5b1ef4e2cee78..97c6b5113a18b 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -78,8 +78,6 @@ Indexing - Bug in :meth:`Series.reset_index` where appropriate error was not raised with an invalid level name (:issue:`20925`) - Bug in :func:`interval_range` when ``start``/``periods`` or ``end``/``periods`` are specified with float ``start`` or ``end`` (:issue:`21161`) -- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a ``MultiIndex`` with ``nlevels == 1`` (:issue:`21149`) -- I/O ^^^ From bf4494f84821794c6c8122b4111352e90af1cee1 Mon Sep 17 00:00:00 2001 From: Kalyan Gokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Wed, 13 Jun 2018 22:17:15 +0530 Subject: [PATCH 08/11] Removed changes to whatsnew v0.23.1 --- doc/source/whatsnew/v0.23.1.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index 97c6b5113a18b..395c0cab1a2f4 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -79,6 +79,7 @@ Indexing - Bug in :meth:`Series.reset_index` where appropriate error was not raised with an invalid level name (:issue:`20925`) - Bug in :func:`interval_range` when ``start``/``periods`` or ``end``/``periods`` are specified with float ``start`` or ``end`` (:issue:`21161`) + I/O ^^^ From 69257323ff946f6b71c33589271bec457d049d5b Mon Sep 17 00:00:00 2001 From: Kalyan Gokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Wed, 13 Jun 2018 22:17:55 +0530 Subject: [PATCH 09/11] Removed changes from whatsnew v0.23.1 --- doc/source/whatsnew/v0.23.1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index 395c0cab1a2f4..a071d7f3f5534 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -78,7 +78,7 @@ Indexing - Bug in :meth:`Series.reset_index` where appropriate error was not raised with an invalid level name (:issue:`20925`) - Bug in :func:`interval_range` when ``start``/``periods`` or ``end``/``periods`` are specified with float ``start`` or ``end`` (:issue:`21161`) - +- I/O ^^^ From a27cc983f249bf91f009c51cf7c171a103944694 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Wed, 13 Jun 2018 22:21:14 +0530 Subject: [PATCH 10/11] Updated whatsnew v0.23.2 GH21149-1a --- doc/source/whatsnew/v0.23.2.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.2.txt b/doc/source/whatsnew/v0.23.2.txt index 3e4326dea2ecc..cc84c9e3bf29e 100644 --- a/doc/source/whatsnew/v0.23.2.txt +++ b/doc/source/whatsnew/v0.23.2.txt @@ -52,7 +52,8 @@ Bug Fixes **Indexing** - Bug in :meth:`Index.get_indexer_non_unique` with categorical key (:issue:`21448`) -- +- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with self.nlevels == 1 (:issue:`21149`) +- **I/O** From 73cac750753b318fced58091eca1d99880fa19e4 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Thu, 14 Jun 2018 06:22:48 -0400 Subject: [PATCH 11/11] doc --- doc/source/whatsnew/v0.23.2.txt | 4 ++-- pandas/tests/indexes/test_multi.py | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.23.2.txt b/doc/source/whatsnew/v0.23.2.txt index cc84c9e3bf29e..0d3f9cb8dd3b6 100644 --- a/doc/source/whatsnew/v0.23.2.txt +++ b/doc/source/whatsnew/v0.23.2.txt @@ -52,8 +52,8 @@ Bug Fixes **Indexing** - Bug in :meth:`Index.get_indexer_non_unique` with categorical key (:issue:`21448`) -- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with self.nlevels == 1 (:issue:`21149`) -- +- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with ``nlevels == 1`` (:issue:`21149`) +- **I/O** diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 5a487040f7cf4..ab53002ee1587 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -3310,9 +3310,8 @@ def test_duplicate_multiindex_labels(self): def test_multiindex_compare(self): # GH 21149 - """ Ensure comparison operations for MultiIndex with nlevels == 1 - behave consistently with those for MultiIndex with nlevels > 1 - """ + # Ensure comparison operations for MultiIndex with nlevels == 1 + # behave consistently with those for MultiIndex with nlevels > 1 midx = pd.MultiIndex.from_product([[0, 1]])