Skip to content

REGR: fix calling numpy bitwise ufunc with Index objects #48149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- 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`)
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
- Fixed regression in calling bitwise numpy ufuncs (for example, ``np.bitwise_and``) on Index objects (:issue:`46769`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Fixed regression in calling bitwise numpy ufuncs (for example, ``np.bitwise_and``) on Index objects (:issue:`46769`)
- Fixed regression in calling bitwise numpy ufuncs (for example, ``np.bitwise_and``) on :class:`Index` objects (:issue:`46769`)

- Fixed regression in :meth:`DataFrame.loc` not updating the cache correctly after values were set (:issue:`47867`)
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
- Fixed regression in setting ``None`` or non-string value into a ``string``-dtype Series using a mask (:issue:`47628`)
Expand Down
23 changes: 18 additions & 5 deletions pandas/_libs/ops_dispatch.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ DISPATCHED_UFUNCS = {
"ge",
"remainder",
"matmul",
"or",
"xor",
"and",
# those three are currently set operations, not logical operations, for the
# Index dunder method (but this is deprecated)
# TODO(2.0) this can be uncommented (and DISPATCHED_UFUNCS_NO_INDEX removed)
# once deprecation is enforced in 2.0
# "or",
# "xor",
# "and",
"neg",
"pos",
"abs",
}
DISPATCHED_UFUNCS_NO_INDEX = {
"or",
"xor",
"and",
}
UNARY_UFUNCS = {
"neg",
"pos",
Expand Down Expand Up @@ -61,7 +70,7 @@ REVERSED_NAMES = {


def maybe_dispatch_ufunc_to_dunder_op(
object self, object ufunc, str method, *inputs, **kwargs
object self, object ufunc, str method, *inputs, bint _no_index=True, **kwargs
):
"""
Dispatch a ufunc to the equivalent dunder method.
Expand Down Expand Up @@ -94,7 +103,11 @@ def maybe_dispatch_ufunc_to_dunder_op(
if kwargs or ufunc.nin > 2:
return NotImplemented

if method == "__call__" and op_name in DISPATCHED_UFUNCS:
if method == "__call__" and (
op_name in DISPATCHED_UFUNCS or (
_no_index and op_name in DISPATCHED_UFUNCS_NO_INDEX
)
):

if inputs[0] is self:
name = f"__{op_name}__"
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
return NotImplemented

result = arraylike.maybe_dispatch_ufunc_to_dunder_op(
self, ufunc, method, *inputs, **kwargs
self, ufunc, method, *inputs, _no_index=False, **kwargs
)
if result is not NotImplemented:
return result
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,16 @@ def test_numpy_ufuncs_reductions(index, func, request):
assert isna(expected)
else:
assert result == expected


@pytest.mark.parametrize("func", [np.bitwise_and, np.bitwise_or, np.bitwise_xor])
def test_numpy_ufuncs_bitwise(func):
# https://github.com/pandas-dev/pandas/issues/46769
idx1 = Index([1, 2, 3, 4], dtype="int64")
idx2 = Index([3, 4, 5, 6], dtype="int64")

with tm.assert_produces_warning(None):
result = func(idx1, idx2)

expected = Index(func(idx1.values, idx2.values))
tm.assert_index_equal(result, expected)