Skip to content

Commit 4e0b636

Browse files
makbigcjbrockmendel
authored andcommitted
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
1 parent dbb767c commit 4e0b636

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

doc/source/whatsnew/v0.24.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ Other
803803
- :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`)
804804
- Require at least 0.28.2 version of ``cython`` to support read-only memoryviews (:issue:`21688`)
805805
- :meth:`~pandas.io.formats.style.Styler.background_gradient` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` (:issue:`15204`)
806-
- :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`)
806+
- :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.
807+
- Logical operations ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`)
807808
-
808809
-
809810
-

pandas/core/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ def na_op(x, y):
15331533
if isinstance(y, list):
15341534
y = construct_1d_object_array_from_listlike(y)
15351535

1536-
if isinstance(y, (np.ndarray, ABCSeries)):
1536+
if isinstance(y, (np.ndarray, ABCSeries, ABCIndexClass)):
15371537
if (is_bool_dtype(x.dtype) and is_bool_dtype(y.dtype)):
15381538
result = op(x, y) # when would this be hit?
15391539
else:

pandas/tests/series/test_operators.py

+24
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,30 @@ def test_comparison_flex_alignment_fill(self):
425425
exp = pd.Series([True, True, False, False], index=list('abcd'))
426426
assert_series_equal(left.gt(right, fill_value=0), exp)
427427

428+
def test_logical_ops_with_index(self):
429+
# GH22092
430+
ser = Series([True, True, False, False])
431+
idx1 = Index([True, False, True, False])
432+
idx2 = Index([1, 0, 1, 0])
433+
434+
expected = Series([True, False, False, False])
435+
result1 = ser & idx1
436+
assert_series_equal(result1, expected)
437+
result2 = ser & idx2
438+
assert_series_equal(result2, expected)
439+
440+
expected = Series([True, True, True, False])
441+
result1 = ser | idx1
442+
assert_series_equal(result1, expected)
443+
result2 = ser | idx2
444+
assert_series_equal(result2, expected)
445+
446+
expected = Series([False, True, True, False])
447+
result1 = ser ^ idx1
448+
assert_series_equal(result1, expected)
449+
result2 = ser ^ idx2
450+
assert_series_equal(result2, expected)
451+
428452
def test_ne(self):
429453
ts = Series([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float)
430454
expected = [True, True, False, True, True]

0 commit comments

Comments
 (0)