Skip to content

Commit 7d8626d

Browse files
mingglijreback
authored andcommitted
BUG: Series dot product __rmatmul__ doesn't allow matrix vector multiplication (#21578)
1 parent 922c8ba commit 7d8626d

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Offsets
184184
Numeric
185185
^^^^^^^
186186

187-
-
187+
- Bug in :class:`Series` ``__rmatmul__`` doesn't support matrix vector multiplication (:issue:`21530`)
188188
-
189189
-
190190

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,7 @@ def __matmul__(self, other):
20662066

20672067
def __rmatmul__(self, other):
20682068
""" Matrix multiplication using binary `@` operator in Python>=3.5 """
2069-
return self.dot(other)
2069+
return self.dot(np.transpose(other))
20702070

20712071
@Substitution(klass='Series')
20722072
@Appender(base._shared_docs['searchsorted'])

pandas/tests/series/test_analytics.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -849,11 +849,30 @@ def test_matmul(self):
849849
expected = np.dot(a.values, a.values)
850850
assert_almost_equal(result, expected)
851851

852-
# np.array @ Series (__rmatmul__)
852+
# GH 21530
853+
# vector (1D np.array) @ Series (__rmatmul__)
853854
result = operator.matmul(a.values, a)
854855
expected = np.dot(a.values, a.values)
855856
assert_almost_equal(result, expected)
856857

858+
# GH 21530
859+
# vector (1D list) @ Series (__rmatmul__)
860+
result = operator.matmul(a.values.tolist(), a)
861+
expected = np.dot(a.values, a.values)
862+
assert_almost_equal(result, expected)
863+
864+
# GH 21530
865+
# matrix (2D np.array) @ Series (__rmatmul__)
866+
result = operator.matmul(b.T.values, a)
867+
expected = np.dot(b.T.values, a.values)
868+
assert_almost_equal(result, expected)
869+
870+
# GH 21530
871+
# matrix (2D nested lists) @ Series (__rmatmul__)
872+
result = operator.matmul(b.T.values.tolist(), a)
873+
expected = np.dot(b.T.values, a.values)
874+
assert_almost_equal(result, expected)
875+
857876
# mixed dtype DataFrame @ Series
858877
a['p'] = int(a.p)
859878
result = operator.matmul(b.T, a)

0 commit comments

Comments
 (0)