Skip to content

DOC: updated the docstring of Series.dot #22890

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
50 changes: 46 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2080,16 +2080,58 @@ def autocorr(self, lag=1):

def dot(self, other):
"""
Matrix multiplication with DataFrame or inner-product with Series
objects. Can also be called using `self @ other` in Python >= 3.5.
Compute the dot product between the Series and the columns of other.

This method computes the dot product between the Series and another
one, or the Series and each columns of a DataFrame, or the Series and
each columns of an array.

It can also be called using `self @ other` in Python >= 3.5.

Parameters
----------
other : Series or DataFrame
other : Series, DataFrame or array-like
The other object to compute the dot product with its columns.

Returns
-------
dot_product : scalar or Series
scalar, Series or numpy.ndarray
return the dot product of the Series and other if other is a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you capitalize Return

Series, the Series of the dot product of Series and each rows of
other if other is a DataFrame or a numpy.ndarray between the Series
and each columns of the numpy array.

See Also
--------
DataFrame.dot: Compute dot product with the columns of the DataFrame.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I'd add Series.mul here. Up to you.


Notes
-----
The Series and other has to share the same index if other is a Series
or a DataFrame.

Examples
--------
>>> ser = pd.Series([0,1,2,3])
>>> ser2 = pd.Series([-1,2,-3,4])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use s for the first Series, and other for the second instead? We try to keep them consistent in all docstrings. Or if you think it makes sense, weights instead of other, I think that will help at least people with a machine learning background (but if you use weights probably better to use floats for the values).

Also, can you review PEP-8 of the whole examples? I see that after most commas you're missing a space.

>>> ser.dot(ser2)
8
>>> df = pd.DataFrame([[0,1],[-2,3],[4,-5],[6,7]])
>>> ser.dot(df)
0 24
1 14
dtype: int64
>>> arr = [[0,1],[-2,3],[4,-5],[6,7]]
>>> ser.dot(arr)
array([24, 14])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it probably makes more sense to show an example with a numpy array, more than with Python lists. I don't think (may be I'm wrong) in real cases it's so common to make operations between Python lists and pandas objects.

But I'll leave that to you, whatever you think it makes more sense.


If the Series don't share the same index, the dot product can't be
computed.

>>> ser3 = pd.Series({'a': 0, 'b': 1, 'c': 2, 'd': 3})
>>> ser4 = pd.Series({'d': 0, 'c': 1, 'b': 2, 'a': 3})
>>> ser3.dot(ser4)
4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd remove this last example and the previous comment. While I think it can be very useful, I think that applies to every method of Series and DataFrame, and I don't think we want to show this in every docstring.

"""
from pandas.core.frame import DataFrame
if isinstance(other, (Series, DataFrame)):
Expand Down