Skip to content

Commit b62a07a

Browse files
authored
docs: code samples for Series.dot and DataFrame.dot (#226)
1 parent 3a375e8 commit b62a07a

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

bigframes/dataframe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2797,7 +2797,8 @@ def get_right_id(id):
27972797
result = result[other_frame.columns]
27982798

27992799
if isinstance(other, bf_series.Series):
2800-
result = result[other.name].rename()
2800+
# There should be exactly one column in the result
2801+
result = result[result.columns[0]].rename()
28012802

28022803
return result
28032804

bigframes/operations/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _apply_binary_op(
141141
if isinstance(other, pd.Series):
142142
# TODO: Convert to BigQuery DataFrames series
143143
raise NotImplementedError(
144-
f"Pandas series not supported supported as operand. {constants.FEEDBACK_LINK}"
144+
f"Pandas series not supported as operand. {constants.FEEDBACK_LINK}"
145145
)
146146
if isinstance(other, series.Series):
147147
(left, right, block) = self._align(other, how=alignment)

tests/system/small/test_dataframe.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,6 +3493,29 @@ def test_df_dot_operator(
34933493
)
34943494

34953495

3496+
def test_df_dot_series_inline():
3497+
left = [[1, 2, 3], [2, 5, 7]]
3498+
right = [2, 1, 3]
3499+
3500+
bf1 = dataframe.DataFrame(left)
3501+
bf2 = series.Series(right)
3502+
bf_result = bf1.dot(bf2).to_pandas()
3503+
3504+
df1 = pd.DataFrame(left)
3505+
df2 = pd.Series(right)
3506+
pd_result = df1.dot(df2)
3507+
3508+
# Patch pandas dtypes for testing parity
3509+
# Pandas result is int64 instead of Int64 (nullable) dtype.
3510+
pd_result = pd_result.astype(pd.Int64Dtype())
3511+
pd_result.index = pd_result.index.astype(pd.Int64Dtype())
3512+
3513+
pd.testing.assert_series_equal(
3514+
bf_result,
3515+
pd_result,
3516+
)
3517+
3518+
34963519
def test_df_dot_series(
34973520
matrix_2by3_df, matrix_2by3_pandas_df, matrix_3by4_df, matrix_3by4_pandas_df
34983521
):

third_party/bigframes_vendored/pandas/core/frame.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,6 +3485,77 @@ def dot(self, other):
34853485
The dot method for Series computes the inner product, instead of the
34863486
matrix product here.
34873487
3488+
**Examples:**
3489+
3490+
>>> import bigframes.pandas as bpd
3491+
>>> bpd.options.display.progress_bar = None
3492+
3493+
>>> left = bpd.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]])
3494+
>>> left
3495+
0 1 2 3
3496+
0 0 1 -2 -1
3497+
1 1 1 1 1
3498+
<BLANKLINE>
3499+
[2 rows x 4 columns]
3500+
>>> right = bpd.DataFrame([[0, 1], [1, 2], [-1, -1], [2, 0]])
3501+
>>> right
3502+
0 1
3503+
0 0 1
3504+
1 1 2
3505+
2 -1 -1
3506+
3 2 0
3507+
<BLANKLINE>
3508+
[4 rows x 2 columns]
3509+
>>> left.dot(right)
3510+
0 1
3511+
0 1 4
3512+
1 2 2
3513+
<BLANKLINE>
3514+
[2 rows x 2 columns]
3515+
3516+
You can also use the operator ``@`` for the dot product:
3517+
3518+
>>> left @ right
3519+
0 1
3520+
0 1 4
3521+
1 2 2
3522+
<BLANKLINE>
3523+
[2 rows x 2 columns]
3524+
3525+
The right input can be a Series, in which case the result will also be a
3526+
Series:
3527+
3528+
>>> right = bpd.Series([1, 2, -1,0])
3529+
>>> left @ right
3530+
0 4
3531+
1 2
3532+
dtype: Int64
3533+
3534+
Any user defined index of the left matrix and columns of the right
3535+
matrix will reflect in the result.
3536+
3537+
>>> left = bpd.DataFrame([[1, 2, 3], [2, 5, 7]], index=["alpha", "beta"])
3538+
>>> left
3539+
0 1 2
3540+
alpha 1 2 3
3541+
beta 2 5 7
3542+
<BLANKLINE>
3543+
[2 rows x 3 columns]
3544+
>>> right = bpd.DataFrame([[2, 4, 8], [1, 5, 10], [3, 6, 9]], columns=["red", "green", "blue"])
3545+
>>> right
3546+
red green blue
3547+
0 2 4 8
3548+
1 1 5 10
3549+
2 3 6 9
3550+
<BLANKLINE>
3551+
[3 rows x 3 columns]
3552+
>>> left.dot(right)
3553+
red green blue
3554+
alpha 13 32 55
3555+
beta 30 75 129
3556+
<BLANKLINE>
3557+
[2 rows x 3 columns]
3558+
34883559
Args:
34893560
other (Series or DataFrame):
34903561
The other object to compute the matrix product with.

third_party/bigframes_vendored/pandas/core/series.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,21 @@ def dot(self, other) -> Series | np.ndarray:
631631
BigQuery Dataframes does not validate this property and will produce
632632
incorrect results if indices are not equal.
633633
634+
**Examples:**
635+
636+
>>> import bigframes.pandas as bpd
637+
>>> bpd.options.display.progress_bar = None
638+
639+
>>> s = bpd.Series([0, 1, 2, 3])
640+
>>> other = bpd.Series([-1, 2, -3, 4])
641+
>>> s.dot(other)
642+
8
643+
644+
You can also use the operator ``@`` for the dot product:
645+
646+
>>> s @ other
647+
8
648+
634649
Args:
635650
other (Series):
636651
The other object to compute the dot product with its columns.

0 commit comments

Comments
 (0)