Skip to content

BUG: Fix ExtensionArray binary op protocol #61990

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def _logical_method(self, other, op): # type: ignore[override]
elif is_list_like(other):
other = np.asarray(other, dtype="bool")
if other.ndim > 1:
raise NotImplementedError("can only perform ops with 1-d structures")
return NotImplemented
other, mask = coerce_to_array(other, copy=False)
elif isinstance(other, np.bool_):
other = other.item()
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,27 @@ def test_modulo_zero_int(self):
expected = Series([np.nan, 0.0])
tm.assert_series_equal(result, expected)

def test_np_array_mul_ea_array_returns_extensionarray(self):
# GH#61866
np_array = np.array([1, 2, 3, 4, 5], dtype=np.int64)
ea_array = array([1, 2, 3, 4, 5], dtype="Int64")
result = np_array * ea_array
assert isinstance(result, type(ea_array))
tm.assert_equal(result, array([1, 4, 9, 16, 25], dtype="Int64"))
Copy link
Member

Choose a reason for hiding this comment

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

is this actually affected by this PR? if not i expect it is already tested?

Copy link
Contributor Author

@tisjayy tisjayy Jul 31, 2025

Choose a reason for hiding this comment

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

Test checks that multiplying a NumPy array by an ExtensionArray gives the correct type (an ExtensionArray), which means the fallback via NotImplemented is working as intended. I couldnt find anything similar in test_numeric.py or some other files i tried to look into.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you think its checked elsewhere, i will remove it


def test_non_1d_ea_raises_notimplementederror(self):
# GH#61866
ea_array = array([1, 2, 3, 4, 5], dtype="Int64").reshape(5, 1)
np_array = np.array([1, 2, 3, 4, 5], dtype=np.int64).reshape(5, 1)

msg = "can only perform ops with 1-d structures"

with pytest.raises(NotImplementedError, match=msg):
ea_array * np_array

with pytest.raises(NotImplementedError, match=msg):
np_array * ea_array


class TestAdditionSubtraction:
# __add__, __sub__, __radd__, __rsub__, __iadd__, __isub__
Expand Down
Loading