GeezerVison is showing github.com — feat: Add bigframes.pandas.col with basic operators (#2405) · google/bigframes@1274167
File treeExpand file treeCollapse file tree Open diff view settings third_party/bigframes_vendored/pandas/core
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bigframes/core/agg_expressions.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import functools | 20 | 20 | import itertools | 21 | 21 | import typing | 22 | | -from typing import Callable, Mapping, Tuple, TypeVar | | 22 | +from typing import Callable, Hashable, Mapping, Tuple, TypeVar | 23 | 23 |
| 24 | 24 | from bigframes import dtypes | 25 | 25 | from bigframes.core import expression, window_spec | @@ -68,7 +68,7 @@ def children(self) -> Tuple[expression.Expression, ...]: | 68 | 68 | return self.inputs | 69 | 69 |
| 70 | 70 | @property | 71 | | - def free_variables(self) -> typing.Tuple[str, ...]: | | 71 | + def free_variables(self) -> typing.Tuple[Hashable, ...]: | 72 | 72 | return tuple( | 73 | 73 | itertools.chain.from_iterable(map(lambda x: x.free_variables, self.inputs)) | 74 | 74 | ) | @@ -92,7 +92,7 @@ def transform_children( | 92 | 92 |
| 93 | 93 | def bind_variables( | 94 | 94 | self: TExpression, | 95 | | - bindings: Mapping[str, expression.Expression], | | 95 | + bindings: Mapping[Hashable, expression.Expression], | 96 | 96 | allow_partial_bindings: bool = False, | 97 | 97 | ) -> TExpression: | 98 | 98 | return self.transform_children( | @@ -192,7 +192,7 @@ def children(self) -> Tuple[expression.Expression, ...]: | 192 | 192 | return self.inputs | 193 | 193 |
| 194 | 194 | @property | 195 | | - def free_variables(self) -> typing.Tuple[str, ...]: | | 195 | + def free_variables(self) -> typing.Tuple[Hashable, ...]: | 196 | 196 | return tuple( | 197 | 197 | itertools.chain.from_iterable(map(lambda x: x.free_variables, self.inputs)) | 198 | 198 | ) | @@ -216,7 +216,7 @@ def transform_children( | 216 | 216 |
| 217 | 217 | def bind_variables( | 218 | 218 | self: WindowExpression, | 219 | | - bindings: Mapping[str, expression.Expression], | | 219 | + bindings: Mapping[Hashable, expression.Expression], | 220 | 220 | allow_partial_bindings: bool = False, | 221 | 221 | ) -> WindowExpression: | 222 | 222 | return self.transform_children( | |
Collapse file Copy file name to clipboard +126Lines changed: 126 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2026 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# http://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | +from __future__ import annotations | | 15 | + | | 16 | +import dataclasses | | 17 | +from typing import Any, Hashable | | 18 | + | | 19 | +import bigframes_vendored.pandas.core.col as pd_col | | 20 | + | | 21 | +import bigframes.core.expression as bf_expression | | 22 | +import bigframes.operations as bf_ops | | 23 | + | | 24 | + | | 25 | +# Not to be confused with the Expression class in `bigframes.core.expressions` | | 26 | +# Name collision unintended | | 27 | +@dataclasses.dataclass(frozen=True) | | 28 | +class Expression: | | 29 | + __doc__ = pd_col.Expression.__doc__ | | 30 | + | | 31 | + _value: bf_expression.Expression | | 32 | + | | 33 | + def _apply_unary(self, op: bf_ops.UnaryOp) -> Expression: | | 34 | + return Expression(op.as_expr(self._value)) | | 35 | + | | 36 | + def _apply_binary(self, other: Any, op: bf_ops.BinaryOp, reverse: bool = False): | | 37 | + if isinstance(other, Expression): | | 38 | + other_value = other._value | | 39 | + else: | | 40 | + other_value = bf_expression.const(other) | | 41 | + if reverse: | | 42 | + return Expression(op.as_expr(other_value, self._value)) | | 43 | + else: | | 44 | + return Expression(op.as_expr(self._value, other_value)) | | 45 | + | | 46 | + def __add__(self, other: Any) -> Expression: | | 47 | + return self._apply_binary(other, bf_ops.add_op) | | 48 | + | | 49 | + def __radd__(self, other: Any) -> Expression: | | 50 | + return self._apply_binary(other, bf_ops.add_op, reverse=True) | | 51 | + | | 52 | + def __sub__(self, other: Any) -> Expression: | | 53 | + return self._apply_binary(other, bf_ops.sub_op) | | 54 | + | | 55 | + def __rsub__(self, other: Any) -> Expression: | | 56 | + return self._apply_binary(other, bf_ops.sub_op, reverse=True) | | 57 | + | | 58 | + def __mul__(self, other: Any) -> Expression: | | 59 | + return self._apply_binary(other, bf_ops.mul_op) | | 60 | + | | 61 | + def __rmul__(self, other: Any) -> Expression: | | 62 | + return self._apply_binary(other, bf_ops.mul_op, reverse=True) | | 63 | + | | 64 | + def __truediv__(self, other: Any) -> Expression: | | 65 | + return self._apply_binary(other, bf_ops.div_op) | | 66 | + | | 67 | + def __rtruediv__(self, other: Any) -> Expression: | | 68 | + return self._apply_binary(other, bf_ops.div_op, reverse=True) | | 69 | + | | 70 | + def __floordiv__(self, other: Any) -> Expression: | | 71 | + return self._apply_binary(other, bf_ops.floordiv_op) | | 72 | + | | 73 | + def __rfloordiv__(self, other: Any) -> Expression: | | 74 | + return self._apply_binary(other, bf_ops.floordiv_op, reverse=True) | | 75 | + | | 76 | + def __ge__(self, other: Any) -> Expression: | | 77 | + return self._apply_binary(other, bf_ops.ge_op) | | 78 | + | | 79 | + def __gt__(self, other: Any) -> Expression: | | 80 | + return self._apply_binary(other, bf_ops.gt_op) | | 81 | + | | 82 | + def __le__(self, other: Any) -> Expression: | | 83 | + return self._apply_binary(other, bf_ops.le_op) | | 84 | + | | 85 | + def __lt__(self, other: Any) -> Expression: | | 86 | + return self._apply_binary(other, bf_ops.lt_op) | | 87 | + | | 88 | + def __eq__(self, other: object) -> Expression: # type: ignore | | 89 | + return self._apply_binary(other, bf_ops.eq_op) | | 90 | + | | 91 | + def __ne__(self, other: object) -> Expression: # type: ignore | | 92 | + return self._apply_binary(other, bf_ops.ne_op) | | 93 | + | | 94 | + def __mod__(self, other: Any) -> Expression: | | 95 | + return self._apply_binary(other, bf_ops.mod_op) | | 96 | + | | 97 | + def __rmod__(self, other: Any) -> Expression: | | 98 | + return self._apply_binary(other, bf_ops.mod_op, reverse=True) | | 99 | + | | 100 | + def __and__(self, other: Any) -> Expression: | | 101 | + return self._apply_binary(other, bf_ops.and_op) | | 102 | + | | 103 | + def __rand__(self, other: Any) -> Expression: | | 104 | + return self._apply_binary(other, bf_ops.and_op, reverse=True) | | 105 | + | | 106 | + def __or__(self, other: Any) -> Expression: | | 107 | + return self._apply_binary(other, bf_ops.or_op) | | 108 | + | | 109 | + def __ror__(self, other: Any) -> Expression: | | 110 | + return self._apply_binary(other, bf_ops.or_op, reverse=True) | | 111 | + | | 112 | + def __xor__(self, other: Any) -> Expression: | | 113 | + return self._apply_binary(other, bf_ops.xor_op) | | 114 | + | | 115 | + def __rxor__(self, other: Any) -> Expression: | | 116 | + return self._apply_binary(other, bf_ops.xor_op, reverse=True) | | 117 | + | | 118 | + def __invert__(self) -> Expression: | | 119 | + return self._apply_unary(bf_ops.invert_op) | | 120 | + | | 121 | + | | 122 | +def col(col_name: Hashable) -> Expression: | | 123 | + return Expression(bf_expression.free_var(col_name)) | | 124 | + | | 125 | + | | 126 | +col.__doc__ = pd_col.col.__doc__ |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/expression.py +21-11Lines changed: 21 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import functools | 20 | 20 | import itertools | 21 | 21 | import typing | 22 | | -from typing import Callable, Generator, Mapping, TypeVar, Union | | 22 | +from typing import Callable, Generator, Hashable, Mapping, TypeVar, Union | 23 | 23 |
| 24 | 24 | import pandas as pd | 25 | 25 |
| @@ -39,7 +39,7 @@ def deref(name: str) -> DerefOp: | 39 | 39 | return DerefOp(ids.ColumnId(name)) | 40 | 40 |
| 41 | 41 |
| 42 | | -def free_var(id: str) -> UnboundVariableExpression: | | 42 | +def free_var(id: Hashable) -> UnboundVariableExpression: | 43 | 43 | return UnboundVariableExpression(id) | 44 | 44 |
| 45 | 45 |
| @@ -52,7 +52,7 @@ class Expression(abc.ABC): | 52 | 52 | """An expression represents a computation taking N scalar inputs and producing a single output scalar.""" | 53 | 53 |
| 54 | 54 | @property | 55 | | - def free_variables(self) -> typing.Tuple[str, ...]: | | 55 | + def free_variables(self) -> typing.Tuple[Hashable, ...]: | 56 | 56 | return () | 57 | 57 |
| 58 | 58 | @property | @@ -116,7 +116,9 @@ def bind_refs( | 116 | 116 |
| 117 | 117 | @abc.abstractmethod | 118 | 118 | def bind_variables( | 119 | | - self, bindings: Mapping[str, Expression], allow_partial_bindings: bool = False | | 119 | + self, | | 120 | + bindings: Mapping[Hashable, Expression], | | 121 | + allow_partial_bindings: bool = False, | 120 | 122 | ) -> Expression: | 121 | 123 | """Replace variables with expression given in `bindings`. | 122 | 124 |
| @@ -191,7 +193,9 @@ def output_type(self) -> dtypes.ExpressionType: | 191 | 193 | return self.dtype | 192 | 194 |
| 193 | 195 | def bind_variables( | 194 | | - self, bindings: Mapping[str, Expression], allow_partial_bindings: bool = False | | 196 | + self, | | 197 | + bindings: Mapping[Hashable, Expression], | | 198 | + allow_partial_bindings: bool = False, | 195 | 199 | ) -> Expression: | 196 | 200 | return self | 197 | 201 |
| @@ -226,10 +230,10 @@ def transform_children(self, t: Callable[[Expression], Expression]) -> Expressio | 226 | 230 | class UnboundVariableExpression(Expression): | 227 | 231 | """A variable expression representing an unbound variable.""" | 228 | 232 |
| 229 | | - id: str | | 233 | + id: Hashable | 230 | 234 |
| 231 | 235 | @property | 232 | | - def free_variables(self) -> typing.Tuple[str, ...]: | | 236 | + def free_variables(self) -> typing.Tuple[Hashable, ...]: | 233 | 237 | return (self.id,) | 234 | 238 |
| 235 | 239 | @property | @@ -256,7 +260,9 @@ def bind_refs( | 256 | 260 | return self | 257 | 261 |
| 258 | 262 | def bind_variables( | 259 | | - self, bindings: Mapping[str, Expression], allow_partial_bindings: bool = False | | 263 | + self, | | 264 | + bindings: Mapping[Hashable, Expression], | | 265 | + allow_partial_bindings: bool = False, | 260 | 266 | ) -> Expression: | 261 | 267 | if self.id in bindings.keys(): | 262 | 268 | return bindings[self.id] | @@ -304,7 +310,9 @@ def output_type(self) -> dtypes.ExpressionType: | 304 | 310 | raise ValueError(f"Type of variable {self.id} has not been fixed.") | 305 | 311 |
| 306 | 312 | def bind_variables( | 307 | | - self, bindings: Mapping[str, Expression], allow_partial_bindings: bool = False | | 313 | + self, | | 314 | + bindings: Mapping[Hashable, Expression], | | 315 | + allow_partial_bindings: bool = False, | 308 | 316 | ) -> Expression: | 309 | 317 | return self | 310 | 318 |
| @@ -373,7 +381,7 @@ def column_references( | 373 | 381 | ) | 374 | 382 |
| 375 | 383 | @property | 376 | | - def free_variables(self) -> typing.Tuple[str, ...]: | | 384 | + def free_variables(self) -> typing.Tuple[Hashable, ...]: | 377 | 385 | return tuple( | 378 | 386 | itertools.chain.from_iterable(map(lambda x: x.free_variables, self.inputs)) | 379 | 387 | ) | @@ -408,7 +416,9 @@ def output_type(self) -> dtypes.ExpressionType: | 408 | 416 | return self.op.output_type(*input_types) | 409 | 417 |
| 410 | 418 | def bind_variables( | 411 | | - self, bindings: Mapping[str, Expression], allow_partial_bindings: bool = False | | 419 | + self, | | 420 | + bindings: Mapping[Hashable, Expression], | | 421 | + allow_partial_bindings: bool = False, | 412 | 422 | ) -> OpExpression: | 413 | 423 | return OpExpression( | 414 | 424 | self.op, | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/dataframe.py +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 58 | 58 | from bigframes.core import agg_expressions | 59 | 59 | import bigframes.core.block_transforms as block_ops | 60 | 60 | import bigframes.core.blocks as blocks | | 61 | +import bigframes.core.col | 61 | 62 | import bigframes.core.convert | 62 | 63 | import bigframes.core.explode | 63 | 64 | import bigframes.core.expression as ex | | 94 | 95 | import bigframes.session | 95 | 96 |
| 96 | 97 | SingleItemValue = Union[ | 97 | | - bigframes.series.Series, int, float, str, pandas.Timedelta, Callable | | 98 | + bigframes.series.Series, | | 99 | + int, | | 100 | + float, | | 101 | + str, | | 102 | + pandas.Timedelta, | | 103 | + Callable, | | 104 | + bigframes.core.col.Expression, | 98 | 105 | ] | 99 | 106 | MultiItemValue = Union[ | 100 | 107 | "DataFrame", Sequence[int | float | str | pandas.Timedelta | Callable] | @@ -2236,6 +2243,13 @@ def _assign_single_item( | 2236 | 2243 | ) -> DataFrame: | 2237 | 2244 | if isinstance(v, bigframes.series.Series): | 2238 | 2245 | return self._assign_series_join_on_index(k, v) | | 2246 | + elif isinstance(v, bigframes.core.col.Expression): | | 2247 | + label_to_col_ref = { | | 2248 | + label: ex.deref(id) for id, label in self._block.col_id_to_label.items() | | 2249 | + } | | 2250 | + resolved_expr = v._value.bind_variables(label_to_col_ref) | | 2251 | + block = self._block.project_block_exprs([resolved_expr], labels=[k]) | | 2252 | + return DataFrame(block) | 2239 | 2253 | elif isinstance(v, bigframes.dataframe.DataFrame): | 2240 | 2254 | v_df_col_count = len(v._block.value_columns) | 2241 | 2255 | if v_df_col_count != 1: | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/pandas/__init__.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 27 | 27 | import pandas | 28 | 28 |
| 29 | 29 | import bigframes._config as config | | 30 | +from bigframes.core.col import col | 30 | 31 | import bigframes.core.global_session as global_session | 31 | 32 | import bigframes.core.indexes | 32 | 33 | from bigframes.core.logging import log_adapter | @@ -415,6 +416,7 @@ def reset_session(): | 415 | 416 | "clean_up_by_session_id", | 416 | 417 | "concat", | 417 | 418 | "crosstab", | | 419 | + "col", | 418 | 420 | "cut", | 419 | 421 | "deploy_remote_function", | 420 | 422 | "deploy_udf", | |
|