Skip to content

BUG: np.matmul with Index raising TypeError #57079

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 2 commits into from
Jan 29, 2024
Merged
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 doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Timezones

Numeric
^^^^^^^
-
- Bug in ``np.matmul`` with :class:`Index` inputs raising a ``TypeError`` (:issue:`57079`)
-

Conversion
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,9 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
elif method == "reduce":
result = lib.item_from_zerodim(result)
return result
elif is_scalar(result):
# e.g. matmul
return result

if result.dtype == np.float16:
result = result.astype(np.float32)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,13 @@ def test_np_matmul():
tm.assert_frame_equal(expected, result)


@pytest.mark.parametrize("box", [pd.Index, pd.Series])
def test_np_matmul_1D(box):
result = np.matmul(box([1, 2]), box([2, 3]))
assert result == 8
assert isinstance(result, np.int64)


def test_array_ufuncs_for_many_arguments():
# GH39853
def add3(x, y, z):
Expand Down