Skip to content

feat: Implement operator @ for DataFrame.dot #139

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
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2707,3 +2707,5 @@ def get_right_id(id):
result = result[other.name].rename()

return result

__matmul__ = dot
33 changes: 33 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3264,6 +3264,23 @@ def test_df_dot(
)


def test_df_dot_operator(
matrix_2by3_df, matrix_2by3_pandas_df, matrix_3by4_df, matrix_3by4_pandas_df
):
bf_result = (matrix_2by3_df @ matrix_3by4_df).to_pandas()
pd_result = matrix_2by3_pandas_df @ matrix_3by4_pandas_df

# Patch pandas dtypes for testing parity
# Pandas result is object instead of Int64 (nullable) dtype.
for name in pd_result.columns:
pd_result[name] = pd_result[name].astype(pd.Int64Dtype())

pd.testing.assert_frame_equal(
bf_result,
pd_result,
)


def test_df_dot_series(
matrix_2by3_df, matrix_2by3_pandas_df, matrix_3by4_df, matrix_3by4_pandas_df
):
Expand All @@ -3278,3 +3295,19 @@ def test_df_dot_series(
bf_result,
pd_result,
)


def test_df_dot_operator_series(
matrix_2by3_df, matrix_2by3_pandas_df, matrix_3by4_df, matrix_3by4_pandas_df
):
bf_result = (matrix_2by3_df @ matrix_3by4_df["x"]).to_pandas()
pd_result = matrix_2by3_pandas_df @ matrix_3by4_pandas_df["x"]

# Patch pandas dtypes for testing parity
# Pandas result is object instead of Int64 (nullable) dtype.
pd_result = pd_result.astype(pd.Int64Dtype())

pd.testing.assert_series_equal(
bf_result,
pd_result,
)
16 changes: 16 additions & 0 deletions tests/system/small/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,13 +998,19 @@ def test_df_multi_index_dot_not_supported():
with pytest.raises(NotImplementedError, match="Multi-index input is not supported"):
bf1.dot(bf2)

with pytest.raises(NotImplementedError, match="Multi-index input is not supported"):
bf1 @ bf2

# right multi-index
right_index = pandas.MultiIndex.from_tuples([("a", "aa"), ("a", "ab"), ("b", "bb")])
bf1 = bpd.DataFrame(left_matrix)
bf2 = bpd.DataFrame(right_matrix, index=right_index)
with pytest.raises(NotImplementedError, match="Multi-index input is not supported"):
bf1.dot(bf2)

with pytest.raises(NotImplementedError, match="Multi-index input is not supported"):
bf1 @ bf2


def test_column_multi_index_dot_not_supported():
left_matrix = [[1, 2, 3], [2, 5, 7]]
Expand All @@ -1022,10 +1028,20 @@ def test_column_multi_index_dot_not_supported():
):
bf1.dot(bf2)

with pytest.raises(
NotImplementedError, match="Multi-level column input is not supported"
):
bf1 @ bf2

# right multi-columns
bf1 = bpd.DataFrame(left_matrix)
bf2 = bpd.DataFrame(right_matrix, columns=multi_level_columns)
with pytest.raises(
NotImplementedError, match="Multi-level column input is not supported"
):
bf1.dot(bf2)

with pytest.raises(
NotImplementedError, match="Multi-level column input is not supported"
):
bf1 @ bf2