Skip to content

Commit 6de74dd

Browse files
authored
BUG: np.matmul with Index raising TypeError (#57079)
1 parent 28e8f8d commit 6de74dd

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Timezones
142142

143143
Numeric
144144
^^^^^^^
145-
-
145+
- Bug in ``np.matmul`` with :class:`Index` inputs raising a ``TypeError`` (:issue:`57079`)
146146
-
147147

148148
Conversion

pandas/core/indexes/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,9 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
951951
elif method == "reduce":
952952
result = lib.item_from_zerodim(result)
953953
return result
954+
elif is_scalar(result):
955+
# e.g. matmul
956+
return result
954957

955958
if result.dtype == np.float16:
956959
result = result.astype(np.float32)

pandas/tests/series/test_ufunc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,13 @@ def test_np_matmul():
427427
tm.assert_frame_equal(expected, result)
428428

429429

430+
@pytest.mark.parametrize("box", [pd.Index, pd.Series])
431+
def test_np_matmul_1D(box):
432+
result = np.matmul(box([1, 2]), box([2, 3]))
433+
assert result == 8
434+
assert isinstance(result, np.int64)
435+
436+
430437
def test_array_ufuncs_for_many_arguments():
431438
# GH39853
432439
def add3(x, y, z):

0 commit comments

Comments
 (0)