From 79b8763ac0994228105f840b7f60bc840e0d776b Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 18 Sep 2018 21:54:14 +0800 Subject: [PATCH] Bug: Logical operator of Series with Index (#22092) (#22293) * Fix bug #GH22092 * Update v0.24.0.txt * Update v0.24.0.txt * Update ops.py * Update test_operators.py * Update v0.24.0.txt * Update test_operators.py --- doc/source/whatsnew/v0.24.0.txt | 3 ++- pandas/core/ops.py | 2 +- pandas/tests/series/test_operators.py | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 1b8e5757a15fd..39ed5d968707b 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -803,7 +803,8 @@ 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`) -- :meth:`~pandas.io.formats.style.Styler.bar` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` and setting clipping range with ``vmin`` and ``vmax``. ``NaN`` values are also handled properly. (:issue:`21548`, :issue:`21526`) +- :meth:`~pandas.io.formats.style.Styler.bar` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` and setting clipping range with ``vmin`` and ``vmax`` (:issue:`21548` and :issue:`21526`). ``NaN`` values are also handled properly. +- Logical operations ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`) - - - diff --git a/pandas/core/ops.py b/pandas/core/ops.py index ca9c2528f0aef..a7fc2839ea101 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -1533,7 +1533,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, ABCIndexClass)): 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 5e5e9c0895ccf..615f0c9247bd8 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -425,6 +425,30 @@ 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_logical_ops_with_index(self): + # GH22092 + ser = Series([True, True, False, False]) + idx1 = Index([True, False, True, False]) + idx2 = Index([1, 0, 1, 0]) + + expected = Series([True, False, False, False]) + result1 = ser & idx1 + assert_series_equal(result1, expected) + result2 = ser & idx2 + assert_series_equal(result2, expected) + + expected = Series([True, True, True, False]) + result1 = ser | idx1 + assert_series_equal(result1, expected) + result2 = ser | idx2 + assert_series_equal(result2, expected) + + expected = Series([False, True, True, False]) + 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) expected = [True, True, False, True, True]