Skip to content

Commit 5c91a0e

Browse files
Backport PR pandas-dev#48149 on branch 1.4.x (REGR: fix calling numpy bitwise ufunc with Index objects) (pandas-dev#48200)
Backport PR pandas-dev#48149: REGR: fix calling numpy bitwise ufunc with Index objects Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 145a341 commit 5c91a0e

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

doc/source/whatsnew/v1.4.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression in taking NULL :class:`objects` from a :class:`DataFrame` causing a segmentation violation. These NULL values are created by :meth:`numpy.empty_like` (:issue:`46848`)
1818
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
19+
- Fixed regression in calling bitwise numpy ufuncs (for example, ``np.bitwise_and``) on Index objects (:issue:`46769`)
1920
- Fixed regression in :func:`cut` using a ``datetime64`` IntervalIndex as bins (:issue:`46218`)
2021
- Fixed regression in :meth:`DataFrame.select_dtypes` where ``include="number"`` included :class:`BooleanDtype` (:issue:`46870`)
2122
- Fixed regression in :meth:`DataFrame.loc` not updating the cache correctly after values were set (:issue:`47867`)

pandas/core/indexes/base.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -897,11 +897,19 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
897897
if any(isinstance(other, (ABCSeries, ABCDataFrame)) for other in inputs):
898898
return NotImplemented
899899

900-
result = arraylike.maybe_dispatch_ufunc_to_dunder_op(
901-
self, ufunc, method, *inputs, **kwargs
902-
)
903-
if result is not NotImplemented:
904-
return result
900+
# TODO(2.0) the 'and', 'or' and 'xor' dunder methods are currently set
901+
# operations and not logical operations, so don't dispatch
902+
# This is deprecated, so this full 'if' clause can be removed once
903+
# deprecation is enforced in 2.0
904+
if not (
905+
method == "__call__"
906+
and ufunc in (np.bitwise_and, np.bitwise_or, np.bitwise_xor)
907+
):
908+
result = arraylike.maybe_dispatch_ufunc_to_dunder_op(
909+
self, ufunc, method, *inputs, **kwargs
910+
)
911+
if result is not NotImplemented:
912+
return result
905913

906914
if "out" in kwargs:
907915
# e.g. test_dti_isub_tdi

pandas/tests/indexes/test_numpy_compat.py

+13
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,16 @@ def test_numpy_ufuncs_reductions(index, func, request):
146146
assert isna(expected)
147147
else:
148148
assert result == expected
149+
150+
151+
@pytest.mark.parametrize("func", [np.bitwise_and, np.bitwise_or, np.bitwise_xor])
152+
def test_numpy_ufuncs_bitwise(func):
153+
# https://github.com/pandas-dev/pandas/issues/46769
154+
idx1 = Index([1, 2, 3, 4], dtype="int64")
155+
idx2 = Index([3, 4, 5, 6], dtype="int64")
156+
157+
with tm.assert_produces_warning(None):
158+
result = func(idx1, idx2)
159+
160+
expected = Index(func(idx1.values, idx2.values))
161+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)