This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
feat: Add bigframes.pandas.col with basic operators #2405
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e102cdb
feat: Add bigframes.pandas.col with basic operators
TrevorBergeron 98fe262
Merge remote-tracking branch 'github/main' into pd_expressions
TrevorBergeron 313ebbe
add docstrings
TrevorBergeron a64a677
revert common.py
TrevorBergeron a7cd91e
Merge remote-tracking branch 'github/main' into pd_expressions
TrevorBergeron de10885
add full unit tests
TrevorBergeron 78a85f6
style fixes
TrevorBergeron aa6697f
Merge branch 'main' into pd_expressions
TrevorBergeron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import dataclasses | ||
| from typing import Any, Hashable | ||
|
|
||
| import bigframes_vendored.pandas.core.col as pd_col | ||
|
|
||
| import bigframes.core.expression as bf_expression | ||
| import bigframes.operations as bf_ops | ||
|
|
||
|
|
||
| # Not to be confused with internal Expressions class | ||
| # Name collision unintended | ||
| @dataclasses.dataclass(frozen=True) | ||
| class Expression: | ||
| __doc__ = pd_col.Expression.__doc__ | ||
|
|
||
| _value: bf_expression.Expression | ||
|
|
||
| def _apply_unary(self, op: bf_ops.UnaryOp) -> Expression: | ||
| return Expression(op.as_expr(self._value)) | ||
|
|
||
| def _apply_binary(self, other: Any, op: bf_ops.BinaryOp, reverse: bool = False): | ||
| if isinstance(other, Expression): | ||
| other_value = other._value | ||
| else: | ||
| other_value = bf_expression.const(other) | ||
| if reverse: | ||
| return Expression(op.as_expr(other_value, self._value)) | ||
| else: | ||
| return Expression(op.as_expr(self._value, other_value)) | ||
|
|
||
| def __add__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.add_op) | ||
|
|
||
| def __radd__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.add_op, reverse=True) | ||
|
|
||
| def __sub__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.sub_op) | ||
|
|
||
| def __rsub__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.sub_op, reverse=True) | ||
|
|
||
| def __mul__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.mul_op) | ||
|
|
||
| def __rmul__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.mul_op, reverse=True) | ||
|
|
||
| def __truediv__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.div_op) | ||
|
|
||
| def __rtruediv__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.div_op, reverse=True) | ||
|
|
||
| def __floordiv__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.floordiv_op) | ||
|
|
||
| def __rfloordiv__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.floordiv_op, reverse=True) | ||
|
|
||
| def __ge__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.ge_op) | ||
|
|
||
| def __gt__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.gt_op) | ||
|
|
||
| def __le__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.le_op) | ||
|
|
||
| def __lt__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.lt_op) | ||
|
|
||
| def __eq__(self, other: object) -> Expression: # type: ignore | ||
| return self._apply_binary(other, bf_ops.eq_op) | ||
|
|
||
| def __ne__(self, other: object) -> Expression: # type: ignore | ||
| return self._apply_binary(other, bf_ops.ne_op) | ||
|
|
||
| def __mod__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.mod_op) | ||
|
|
||
| def __rmod__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.mod_op, reverse=True) | ||
|
|
||
| def __and__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.and_op) | ||
|
|
||
| def __rand__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.and_op, reverse=True) | ||
|
|
||
| def __or__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.or_op) | ||
|
|
||
| def __ror__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.or_op, reverse=True) | ||
|
|
||
| def __xor__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.xor_op) | ||
|
|
||
| def __rxor__(self, other: Any) -> Expression: | ||
| return self._apply_binary(other, bf_ops.xor_op, reverse=True) | ||
|
|
||
| def __invert__(self) -> Expression: | ||
| return self._apply_unary(bf_ops.invert_op) | ||
|
|
||
|
|
||
| def col(col_name: Hashable) -> Expression: | ||
| return Expression(bf_expression.free_var(col_name)) | ||
|
|
||
|
|
||
| col.__doc__ = pd_col.col.__doc__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -828,6 +828,25 @@ def test_assign_new_column(scalars_dfs): | |
| assert_frame_equal(bf_result, pd_result) | ||
|
|
||
|
|
||
| def test_assign_using_pd_col(scalars_dfs): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-rhetorical question: what's your thought on providing full test coverage for all the operators you have introduced in this change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tests added |
||
| if pd.__version__.startswith("1.") or pd.__version__.startswith("2."): | ||
| pytest.skip("col expression interface only supported for pandas 3+") | ||
| scalars_df, scalars_pandas_df = scalars_dfs | ||
| bf_kwargs = { | ||
| "new_col_1": 4 - bpd.col("int64_col"), | ||
| "new_col_2": bpd.col("int64_col") / (bpd.col("float64_col") * 0.5), | ||
| } | ||
| pd_kwargs = { | ||
| "new_col_1": 4 - pd.col("int64_col"), # type: ignore | ||
| "new_col_2": pd.col("int64_col") / (pd.col("float64_col") * 0.5), # type: ignore | ||
| } | ||
| df = scalars_df.assign(**bf_kwargs) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style nit: an empty line above to separate "arrange" and "act" block
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| bf_result = df.to_pandas() | ||
| pd_result = scalars_pandas_df.assign(**pd_kwargs) | ||
|
|
||
| assert_frame_equal(bf_result, pd_result) | ||
|
|
||
|
|
||
| def test_assign_new_column_w_loc(scalars_dfs): | ||
| scalars_df, scalars_pandas_df = scalars_dfs | ||
| bf_df = scalars_df.copy() | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
=>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done