From b1f5fcac4d42b0a49279782bc76acf9f1c132f2a Mon Sep 17 00:00:00 2001 From: Moi Date: Sun, 7 Oct 2018 12:58:46 +0200 Subject: [PATCH 1/4] DOC: Updated docstring pandas.DataFrame.dot --- pandas/core/frame.py | 53 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f4b7ccb0fdf5b..4b25eb7006c11 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -953,16 +953,61 @@ def __len__(self): def dot(self, other): """ - Matrix multiplication with DataFrame or Series objects. Can also be - called using `self @ other` in Python >= 3.5. + Compute the matrix mutiplication between the DataFrame and other. + + This method computes the matrix product between the DataFrame and the + values of an other Series, DataFrame or a numpy array. + + It can also be called using `self @ other` in Python >= 3.5. Parameters ---------- - other : DataFrame or Series + other : Series, DataFrame or array-like + The other object to compute the matrix product with. Returns ------- - dot_product : DataFrame or Series + Series or DataFrame + Return the Series of the matrix product between the Dataframe and + other if other is a Series, the Dataframe of the matrix product of + each columns of the DataFrame/np.array and each columns of other + if other is a DataFrame or a numpy.ndarray. + + See Also + -------- + Series.dot: Compute the inner product of a Series with another Series + or the columns of a DataFrame or the columns of a numpy array. + + Notes + ----- + The dimensions of DataFrame and other must be compatible in order to + compute the matrix multiplication. + + The dot method for Series computes the inner product, instead of the + matrix product here. + + Examples + -------- + >>> df = pd.DataFrame([[0 ,1, -2, 3], [4, -5, 6, 7]]) + >>> s = pd.Series([0, 1, 2, 3]) + >>> print(df.dot(s)) + 0 6 + 1 28 + dtype: int64 + >>> other = pd.DataFrame([[0 ,1], [7, 8], [-6, 0], [-1, 2]]) + >>> print(df.dot(other)) + 0 1 + 0 16 14 + 1 -78 -22 + >>> df @ other + 0 1 + 0 16 14 + 1 -78 -22 + >>> arr = np.array([[0, 1], [-2, 3], [4, -5], [6, 7]]) + >>> print(df.dot(arr)) + 0 1 + 0 8 34 + 1 76 8 """ if isinstance(other, (Series, DataFrame)): common = self.columns.union(other.index) From 04268cc785071b5c8c5c528531afb7ee3e2fff9f Mon Sep 17 00:00:00 2001 From: Moi Date: Sun, 7 Oct 2018 13:06:07 +0200 Subject: [PATCH 2/4] DOC: Updated docstring pandas.DataFrame.dot --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4b25eb7006c11..be7810f547a30 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -988,13 +988,13 @@ def dot(self, other): Examples -------- - >>> df = pd.DataFrame([[0 ,1, -2, 3], [4, -5, 6, 7]]) + >>> df = pd.DataFrame([[0, 1, -2, 3], [4, -5, 6, 7]]) >>> s = pd.Series([0, 1, 2, 3]) >>> print(df.dot(s)) 0 6 1 28 dtype: int64 - >>> other = pd.DataFrame([[0 ,1], [7, 8], [-6, 0], [-1, 2]]) + >>> other = pd.DataFrame([[0, 1], [7, 8], [-6, 0], [-1, 2]]) >>> print(df.dot(other)) 0 1 0 16 14 From a7ce6122cdddef23e9de470a8cea1e8508e1dbd2 Mon Sep 17 00:00:00 2001 From: Moi Date: Sun, 2 Dec 2018 16:17:46 +0100 Subject: [PATCH 3/4] minor update --- pandas/core/frame.py | 48 ++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index be7810f547a30..5f232d39d87f8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -968,10 +968,9 @@ def dot(self, other): Returns ------- Series or DataFrame - Return the Series of the matrix product between the Dataframe and - other if other is a Series, the Dataframe of the matrix product of - each columns of the DataFrame/np.array and each columns of other - if other is a DataFrame or a numpy.ndarray. + If other is a Series, return the matrix product between self and + other as a Serie. If other is a DataFrame or a numpy.array, return + the matrix product of self and other in a DataFrame of a np.array. See Also -------- @@ -988,26 +987,37 @@ def dot(self, other): Examples -------- - >>> df = pd.DataFrame([[0, 1, -2, 3], [4, -5, 6, 7]]) - >>> s = pd.Series([0, 1, 2, 3]) - >>> print(df.dot(s)) - 0 6 - 1 28 + Here we multiply a DataFrame with a Series. + + >>> df = pd.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]]) + >>> s = pd.Series([1, 1, 2, 1]) + >>> df.dot(s) + 0 -4 + 1 5 dtype: int64 - >>> other = pd.DataFrame([[0, 1], [7, 8], [-6, 0], [-1, 2]]) - >>> print(df.dot(other)) + + Here we multiply a DataFrame with another DataFrame. + + >>> other = pd.DataFrame([[0 ,1], [1, 2], [-1, -1], [2, 0]]) + >>> df.dot(other) 0 1 - 0 16 14 - 1 -78 -22 + 0 1 4 + 1 2 2 + + Note that the dot method give the same result as @ + >>> df @ other 0 1 - 0 16 14 - 1 -78 -22 - >>> arr = np.array([[0, 1], [-2, 3], [4, -5], [6, 7]]) - >>> print(df.dot(arr)) + 0 1 4 + 1 2 2 + + The dot method works also if other is an np.array. + + >>> arr = np.array([[0 ,1], [1, 2], [-1, -1], [2, 0]]) + >>> df.dot(arr) 0 1 - 0 8 34 - 1 76 8 + 0 1 4 + 1 2 2 """ if isinstance(other, (Series, DataFrame)): common = self.columns.union(other.index) From 997cbe6cf2e440212473bcadf7558afd9fcec996 Mon Sep 17 00:00:00 2001 From: Moi Date: Tue, 4 Dec 2018 22:15:37 +0100 Subject: [PATCH 4/4] corrections --- pandas/core/frame.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 19f6776e144d0..a96a31f2566e4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1051,7 +1051,7 @@ def dot(self, other): This method computes the matrix product between the DataFrame and the values of an other Series, DataFrame or a numpy array. - It can also be called using `self @ other` in Python >= 3.5. + It can also be called using ``self @ other`` in Python >= 3.5. Parameters ---------- @@ -1067,8 +1067,7 @@ def dot(self, other): See Also -------- - Series.dot: Compute the inner product of a Series with another Series - or the columns of a DataFrame or the columns of a numpy array. + Series.dot: Similar method for Series. Notes ----- @@ -1091,7 +1090,7 @@ def dot(self, other): Here we multiply a DataFrame with another DataFrame. - >>> other = pd.DataFrame([[0 ,1], [1, 2], [-1, -1], [2, 0]]) + >>> other = pd.DataFrame([[0, 1], [1, 2], [-1, -1], [2, 0]]) >>> df.dot(other) 0 1 0 1 4 @@ -1106,7 +1105,7 @@ def dot(self, other): The dot method works also if other is an np.array. - >>> arr = np.array([[0 ,1], [1, 2], [-1, -1], [2, 0]]) + >>> arr = np.array([[0, 1], [1, 2], [-1, -1], [2, 0]]) >>> df.dot(arr) 0 1 0 1 4