feat: Support pd.col expressions with .loc and getitem (#2473) · googleapis/python-bigquery-dataframes@ae5c8b3 · GitHub
Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit ae5c8b3

Browse files
feat: Support pd.col expressions with .loc and getitem (#2473)
1 parent f1bbba2 commit ae5c8b3

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

bigframes/core/array_value.py

Lines changed: 6 additions & 1 deletion

bigframes/core/indexers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import pandas as pd
2424

2525
import bigframes.core.blocks
26+
import bigframes.core.col
2627
import bigframes.core.expression as ex
2728
import bigframes.core.guid as guid
2829
import bigframes.core.indexes as indexes
@@ -36,7 +37,11 @@
3637

3738
if typing.TYPE_CHECKING:
3839
LocSingleKey = Union[
39-
bigframes.series.Series, indexes.Index, slice, bigframes.core.scalar.Scalar
40+
bigframes.series.Series,
41+
indexes.Index,
42+
slice,
43+
bigframes.core.scalar.Scalar,
44+
bigframes.core.col.Expression,
4045
]
4146

4247

@@ -309,6 +314,15 @@ def _loc_getitem_series_or_dataframe(
309314
raise NotImplementedError(
310315
f"loc does not yet support indexing with a slice. {constants.FEEDBACK_LINK}"
311316
)
317+
if isinstance(key, bigframes.core.col.Expression):
318+
label_to_col_ref = {
319+
label: ex.deref(id)
320+
for id, label in series_or_dataframe._block.col_id_to_label.items()
321+
}
322+
resolved_expr = key._value.bind_variables(label_to_col_ref)
323+
result = series_or_dataframe.copy()
324+
result._set_block(series_or_dataframe._block.filter(resolved_expr))
325+
return result
312326
if callable(key):
313327
raise NotImplementedError(
314328
f"loc does not yet support indexing with a callable. {constants.FEEDBACK_LINK}"

bigframes/dataframe.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,18 @@ def __getitem__(
623623
): # No return type annotations (like pandas) as type cannot always be determined statically
624624
# NOTE: This implements the operations described in
625625
# https://pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html
626+
import bigframes.core.col
627+
import bigframes.pandas
626628

627-
if isinstance(key, bigframes.series.Series):
629+
if isinstance(key, bigframes.pandas.Series):
628630
return self._getitem_bool_series(key)
629631

630632
if isinstance(key, slice):
631633
return self.iloc[key]
632634

635+
if isinstance(key, bigframes.core.col.Expression):
636+
return self.loc[key]
637+
633638
# TODO(tswast): Fix this pylance warning: Class overlaps "Hashable"
634639
# unsafely and could produce a match at runtime
635640
if isinstance(key, blocks.Label):

tests/unit/test_col.py

Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)