Skip to content

Commit 901b02f

Browse files
REGR: fix calling numpy bitwise ufunc with Index objects (#48149)
1 parent 009d0ad commit 901b02f

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
@@ -939,11 +939,19 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
939939
if any(isinstance(other, (ABCSeries, ABCDataFrame)) for other in inputs):
940940
return NotImplemented
941941

942-
result = arraylike.maybe_dispatch_ufunc_to_dunder_op(
943-
self, ufunc, method, *inputs, **kwargs
944-
)
945-
if result is not NotImplemented:
946-
return result
942+
# TODO(2.0) the 'and', 'or' and 'xor' dunder methods are currently set
943+
# operations and not logical operations, so don't dispatch
944+
# This is deprecated, so this full 'if' clause can be removed once
945+
# deprecation is enforced in 2.0
946+
if not (
947+
method == "__call__"
948+
and ufunc in (np.bitwise_and, np.bitwise_or, np.bitwise_xor)
949+
):
950+
result = arraylike.maybe_dispatch_ufunc_to_dunder_op(
951+
self, ufunc, method, *inputs, **kwargs
952+
)
953+
if result is not NotImplemented:
954+
return result
947955

948956
if "out" in kwargs:
949957
# e.g. test_dti_isub_tdi

pandas/tests/indexes/test_numpy_compat.py

+13
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,16 @@ def test_numpy_ufuncs_reductions(index, func, request):
178178
assert isna(expected)
179179
else:
180180
assert result == expected
181+
182+
183+
@pytest.mark.parametrize("func", [np.bitwise_and, np.bitwise_or, np.bitwise_xor])
184+
def test_numpy_ufuncs_bitwise(func):
185+
# https://github.com/pandas-dev/pandas/issues/46769
186+
idx1 = Index([1, 2, 3, 4], dtype="int64")
187+
idx2 = Index([3, 4, 5, 6], dtype="int64")
188+
189+
with tm.assert_produces_warning(None):
190+
result = func(idx1, idx2)
191+
192+
expected = Index(func(idx1.values, idx2.values))
193+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)