Skip to content

Commit 58e2f36

Browse files
committed
ENH: add DataFrame.dot for matrix multiplication, GH #65
1 parent a92d411 commit 58e2f36

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pandas 0.5.1
4646
- Implement logical (boolean) operators &, |, ^ on DataFrame (GH #347)
4747
- Add `Series.mad`, mean absolute deviation, matching DataFrame
4848
- Add `QuarterEnd` DateOffset (PR #321)
49+
- Add matrix multiplication function `dot` to DataFrame (GH #65)
4950
5051
**Improvements to existing features**
5152

@@ -106,6 +107,7 @@ Thanks
106107
- Marius Cobzarenco
107108
- Jev Kuznetsov
108109
- Dieter Vandenbussche
110+
- rsamson
109111

110112
pandas 0.5.0
111113
============

pandas/core/frame.py

+17
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,23 @@ def __neg__(self):
377377
__le__ = comp_method(operator.le, '__le__')
378378
__ge__ = comp_method(operator.ge, '__ge__')
379379

380+
def dot(self, other):
381+
"""
382+
Matrix multiplication with DataFrame objects. Does no data alignment
383+
384+
Parameters
385+
----------
386+
other : DataFrame
387+
388+
Returns
389+
-------
390+
dot_product : DataFrame
391+
"""
392+
lvals = self.values
393+
rvals = other.values
394+
result = np.dot(lvals, rvals)
395+
return DataFrame(result, index=self.index, columns=other.columns)
396+
380397
#----------------------------------------------------------------------
381398
# IO methods (to / from other formats)
382399

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -3338,6 +3338,19 @@ def test_series_put_names(self):
33383338
for k, v in series.iteritems():
33393339
self.assertEqual(v.name, k)
33403340

3341+
def test_dot(self):
3342+
a = DataFrame(np.random.randn(3, 4), index=['a', 'b', 'c'],
3343+
columns=['p', 'q', 'r', 's'])
3344+
b = DataFrame(np.random.randn(4, 2), index=['p', 'q', 'r', 's'],
3345+
columns=['one', 'two'])
3346+
3347+
result = a.dot(b)
3348+
expected = DataFrame(np.dot(a.values, b.values),
3349+
index=['a', 'b', 'c'],
3350+
columns=['one', 'two'])
3351+
assert_frame_equal(result, expected)
3352+
foo
3353+
33413354
class TestDataFrameJoin(unittest.TestCase):
33423355

33433356
def setUp(self):

0 commit comments

Comments
 (0)