From 2c1bb1ac78cea471af941faf2831adc5840ee32c Mon Sep 17 00:00:00 2001 From: makbigc Date: Sun, 12 Aug 2018 22:00:16 +0800 Subject: [PATCH 1/7] Fix bug #GH22092 --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/core/ops.py | 2 +- pandas/tests/series/test_operators.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 9e3f7ec73f852..183655310d3cf 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -497,6 +497,6 @@ Other - :meth: `~pandas.io.formats.style.Styler.background_gradient` now takes a ``text_color_threshold`` parameter to automatically lighten the text color based on the luminance of the background color. This improves readability with dark background colors without the need to limit the background colormap range. (:issue:`21258`) - Require at least 0.28.2 version of ``cython`` to support read-only memoryviews (:issue:`21688`) - :meth: `~pandas.io.formats.style.Styler.background_gradient` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` (:issue:`15204`) -- +- Bug in the logical operator of :class:`Series` with :class:`Index` (:issue:`22092`) - - diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 1ddf77cf71a11..ae8343ef43d3a 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -1383,7 +1383,7 @@ def na_op(x, y): if isinstance(y, list): y = construct_1d_object_array_from_listlike(y) - if isinstance(y, (np.ndarray, ABCSeries)): + if isinstance(y, (np.ndarray, ABCSeries, ABCIndex)): if (is_bool_dtype(x.dtype) and is_bool_dtype(y.dtype)): result = op(x, y) # when would this be hit? else: diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index ecb74622edf10..3b83bb5059f4d 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -537,6 +537,23 @@ def test_comparison_flex_alignment_fill(self): exp = pd.Series([True, True, False, False], index=list('abcd')) assert_series_equal(left.gt(right, fill_value=0), exp) + def test_comparison_with_index(self): + # GH22092 + ser = Series([True, True, False, False]) + idx = Index([True, False, True, False]) + + expected = Series([True, False, False, False]) + result = ser & idx + assert_series_equal(result, expected) + + expected = Series([True, True, True, False]) + result = ser | idx + assert_series_equal(result, expected) + + expected = Series([False, True, True, False]) + result = ser ^ idx + assert_series_equal(result, expected) + def test_ne(self): ts = Series([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float) expected = [True, True, False, True, True] From 3607834dc8de5d99ae6e393892b4c37f9761080d Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Mon, 13 Aug 2018 20:47:32 +0800 Subject: [PATCH 2/7] Update v0.24.0.txt --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 183655310d3cf..93ec9498b2559 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -497,6 +497,6 @@ Other - :meth: `~pandas.io.formats.style.Styler.background_gradient` now takes a ``text_color_threshold`` parameter to automatically lighten the text color based on the luminance of the background color. This improves readability with dark background colors without the need to limit the background colormap range. (:issue:`21258`) - Require at least 0.28.2 version of ``cython`` to support read-only memoryviews (:issue:`21688`) - :meth: `~pandas.io.formats.style.Styler.background_gradient` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` (:issue:`15204`) -- Bug in the logical operator of :class:`Series` with :class:`Index` (:issue:`22092`) +- Bug in the logical operators handling :class:`Series` and :class:`Index` together (:issue:`22092`) - - From 9f7b586a5200956a6945e079262f0053aaddf6e1 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 14 Aug 2018 22:48:02 +0800 Subject: [PATCH 3/7] Update v0.24.0.txt --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 93ec9498b2559..88844b0844184 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -497,6 +497,6 @@ Other - :meth: `~pandas.io.formats.style.Styler.background_gradient` now takes a ``text_color_threshold`` parameter to automatically lighten the text color based on the luminance of the background color. This improves readability with dark background colors without the need to limit the background colormap range. (:issue:`21258`) - Require at least 0.28.2 version of ``cython`` to support read-only memoryviews (:issue:`21688`) - :meth: `~pandas.io.formats.style.Styler.background_gradient` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` (:issue:`15204`) -- Bug in the logical operators handling :class:`Series` and :class:`Index` together (:issue:`22092`) +- Logical operation ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`) - - From fd0f3818a04d6b5ac094f4983c7b692cdfece54d Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 14 Aug 2018 22:48:53 +0800 Subject: [PATCH 4/7] Update ops.py --- pandas/core/ops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index ae8343ef43d3a..36a9470834eb6 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -41,7 +41,7 @@ from pandas.core.dtypes.generic import ( ABCSeries, ABCDataFrame, ABCPanel, - ABCIndex, + ABCIndex, ABCIndexClass, ABCSparseSeries, ABCSparseArray) @@ -1383,7 +1383,7 @@ def na_op(x, y): if isinstance(y, list): y = construct_1d_object_array_from_listlike(y) - if isinstance(y, (np.ndarray, ABCSeries, ABCIndex)): + if isinstance(y, (np.ndarray, ABCSeries, ABCIndexClass)): if (is_bool_dtype(x.dtype) and is_bool_dtype(y.dtype)): result = op(x, y) # when would this be hit? else: From f9f63fd4325f5fffdf86e2d922e7c35c4d022642 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 14 Aug 2018 22:50:53 +0800 Subject: [PATCH 5/7] Update test_operators.py --- pandas/tests/series/test_operators.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index 3b83bb5059f4d..bc61e5e1208e6 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -540,19 +540,26 @@ def test_comparison_flex_alignment_fill(self): def test_comparison_with_index(self): # GH22092 ser = Series([True, True, False, False]) - idx = Index([True, False, True, False]) + idx1 = Index([True, False, True, False]) + idx2 = Index([1, 0, 1, 0]) expected = Series([True, False, False, False]) - result = ser & idx - assert_series_equal(result, expected) + result1 = ser & idx1 + assert_series_equal(result1, expected) + result2 = ser & idx2 + assert_series_equal(result2, expected) expected = Series([True, True, True, False]) - result = ser | idx - assert_series_equal(result, expected) + result1 = ser | idx1 + assert_series_equal(result1, expected) + result2 = ser | idx2 + assert_series_equal(result2, expected) expected = Series([False, True, True, False]) - result = ser ^ idx - assert_series_equal(result, expected) + result1 = ser ^ idx1 + assert_series_equal(result1, expected) + result2 = ser ^ idx2 + assert_series_equal(result2, expected) def test_ne(self): ts = Series([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float) From ba456e76c8d754a70152aaa56decc4fcf32b202e Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Wed, 22 Aug 2018 22:28:33 +0800 Subject: [PATCH 6/7] Update v0.24.0.txt --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 88844b0844184..d7df4a5a91af7 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -497,6 +497,6 @@ Other - :meth: `~pandas.io.formats.style.Styler.background_gradient` now takes a ``text_color_threshold`` parameter to automatically lighten the text color based on the luminance of the background color. This improves readability with dark background colors without the need to limit the background colormap range. (:issue:`21258`) - Require at least 0.28.2 version of ``cython`` to support read-only memoryviews (:issue:`21688`) - :meth: `~pandas.io.formats.style.Styler.background_gradient` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` (:issue:`15204`) -- Logical operation ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`) +- Logical operations ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`) - - From 84ac1586c3666c3a0de502be08844359e42a135d Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Wed, 22 Aug 2018 22:34:10 +0800 Subject: [PATCH 7/7] Update test_operators.py --- pandas/tests/series/test_operators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index bc61e5e1208e6..a4af4757e8a79 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -537,7 +537,7 @@ def test_comparison_flex_alignment_fill(self): exp = pd.Series([True, True, False, False], index=list('abcd')) assert_series_equal(left.gt(right, fill_value=0), exp) - def test_comparison_with_index(self): + def test_logical_ops_with_index(self): # GH22092 ser = Series([True, True, False, False]) idx1 = Index([True, False, True, False])