Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,17 @@ def rdivmod(self, other) -> Tuple[Series, Series]: # type: ignore
return (self.rfloordiv(other), self.rmod(other))

def __matmul__(self, other):
Comment thread
shobsi marked this conversation as resolved.
Outdated
if isinstance(other, bigframes.dataframe.DataFrame):
return Series(
[
pandas.NA if other[col].isna().any() else (self * other[col]).sum()
for col in other.columns
],
index=other.columns,
name=self.name,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will perform several queries, as each aggregate .any() operation initiates a query. I think we can do this more efficiently.

Copy link
Copy Markdown
Contributor Author

@shobsi shobsi Nov 29, 2023

Choose a reason for hiding this comment

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

Sent an optimization, PTAL. It doesn't apply to multi-index yet due to b/313747368, left a TODO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

in the latest version only the multi-level columns operand implementation is inefficient (which should be a less common use case). Hope we can accept it for now.


# At this point other must be a Series
return (self * other).sum()

dot = __matmul__
Expand Down
18 changes: 18 additions & 0 deletions tests/system/small/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,21 @@ def test_column_multi_index_dot_not_supported():
NotImplementedError, match="Multi-level column input is not supported"
):
bf1 @ bf2


def test_series_dot_df_column_multi_index():
left = [10, 11, 12, 13] # series data
right = [[0, 1, 2], [-2, 3, -4], [4, -5, 6], [6, 7, -8]] # dataframe data

multi_level_columns = pandas.MultiIndex.from_arrays(
[["col0", "col0", "col1"], ["col00", "col01", "col11"]]
)

bf_result = bpd.Series(left) @ bpd.DataFrame(right, columns=multi_level_columns)
pd_result = pandas.Series(left) @ pandas.DataFrame(
right, columns=multi_level_columns
)

pandas.testing.assert_series_equal(
bf_result.to_pandas(), pd_result, check_index_type=False, check_dtype=False
)
21 changes: 21 additions & 0 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,27 @@ def test_dot(scalars_dfs):
assert bf_result == pd_result


def test_dot_df(matrix_3by4_df, matrix_3by4_pandas_df):
bf_result = matrix_3by4_df["w"] @ matrix_3by4_df
pd_result = matrix_3by4_pandas_df["w"] @ matrix_3by4_pandas_df

pd.testing.assert_series_equal(
bf_result.to_pandas(), pd_result, check_index_type=False, check_dtype=False
)


def test_dot_df_with_na(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["int64_too"] @ scalars_df[["int64_col", "int64_too"]]
pd_result = (
scalars_pandas_df["int64_too"] @ scalars_pandas_df[["int64_col", "int64_too"]]
)

pd.testing.assert_series_equal(
bf_result.to_pandas(), pd_result, check_index_type=False, check_dtype=False
)


@pytest.mark.parametrize(
("left", "right", "inclusive"),
[
Expand Down
23 changes: 16 additions & 7 deletions third_party/bigframes_vendored/pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,7 @@ def dot(self, other) -> Series | np.ndarray:
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.
one, or the Series and each columns of a DataFrame.

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

Expand All @@ -646,15 +645,25 @@ def dot(self, other) -> Series | np.ndarray:
>>> s @ other
8

The other operand can be a DataFrame:

>>> other = bpd.DataFrame({"a" : [-1, 2, -3, 4],
... "b" : [-10, 20, -30, 40],
... "c" : [-1, 2, -3, bpd.NA]})
>>> s @ other
a 8.0
b 80.0
c <NA>
dtype: Float64

Args:
other (Series):
other (Series, or DataFrame):
The other object to compute the dot product with its columns.

Returns:
scalar, Series or numpy.ndarray: Return the dot product of the Series
and other if other is a 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.
scalar, Series: Return the dot product of the Series
and other if other is a Series, or the Series of the dot product
of Series and each column of other if other is a DataFrame.


"""
Expand Down