Skip to content

Commit 3cee24b

Browse files
fix: Ensure binops with pandas objects returns bigquery dataframes (#1404)
1 parent 5dae01a commit 3cee24b

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

bigframes/core/indexes/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Index(vendored_pandas_index.Index):
4848
_linked_frame: Union[
4949
bigframes.dataframe.DataFrame, bigframes.series.Series, None
5050
] = None
51+
# Must be above 5000 for pandas to delegate to bigframes for binops
52+
__pandas_priority__ = 12000
5153

5254
# Overrided on __new__ to create subclasses like pandas does
5355
def __new__(

bigframes/dataframe.py

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class DataFrame(vendored_pandas_frame.DataFrame):
103103
__doc__ = vendored_pandas_frame.DataFrame.__doc__
104104
# internal flag to disable cache at all
105105
_disable_cache_override: bool = False
106+
# Must be above 5000 for pandas to delegate to bigframes for binops
107+
__pandas_priority__ = 15000
106108

107109
def __init__(
108110
self,

bigframes/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878

7979
@log_adapter.class_logger
8080
class Series(bigframes.operations.base.SeriesMethods, vendored_pandas_series.Series):
81+
# Must be above 5000 for pandas to delegate to bigframes for binops
82+
__pandas_priority__ = 13000
83+
8184
def __init__(self, *args, **kwargs):
8285
self._query_job: Optional[bigquery.QueryJob] = None
8386
super().__init__(*args, **kwargs)

tests/system/small/test_dataframe.py

+14
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,20 @@ def test_listlike_binop_axis_1_in_memory_data(scalars_dfs, input):
24712471
assert_pandas_df_equal(bf_result, pd_result, check_dtype=False)
24722472

24732473

2474+
@skip_legacy_pandas
2475+
def test_df_reverse_binop_pandas(scalars_dfs):
2476+
scalars_df, scalars_pandas_df = scalars_dfs
2477+
2478+
pd_series = pd.Series([100, 200, 300])
2479+
2480+
df_columns = ["int64_col", "float64_col", "int64_too"]
2481+
2482+
bf_result = pd_series + scalars_df[df_columns].to_pandas()
2483+
pd_result = pd_series + scalars_pandas_df[df_columns]
2484+
2485+
assert_pandas_df_equal(bf_result, pd_result, check_dtype=False)
2486+
2487+
24742488
def test_listlike_binop_axis_1_bf_index(scalars_dfs):
24752489
scalars_df, scalars_pandas_df = scalars_dfs
24762490

tests/system/small/test_series.py

+21
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,27 @@ def test_series_binop_w_other_types(scalars_dfs, other):
16561656
)
16571657

16581658

1659+
@pytest.mark.parametrize(
1660+
("other",),
1661+
[
1662+
([-1.4, 2.3, None],),
1663+
(pd.Index([-1.4, 2.3, None]),),
1664+
(pd.Series([-1.4, 2.3, None], index=[44, 2, 1]),),
1665+
],
1666+
)
1667+
@skip_legacy_pandas
1668+
def test_series_reverse_binop_w_other_types(scalars_dfs, other):
1669+
scalars_df, scalars_pandas_df = scalars_dfs
1670+
1671+
bf_result = (other + scalars_df["int64_col"].head(3)).to_pandas()
1672+
pd_result = other + scalars_pandas_df["int64_col"].head(3)
1673+
1674+
assert_series_equal(
1675+
bf_result,
1676+
pd_result,
1677+
)
1678+
1679+
16591680
@skip_legacy_pandas
16601681
def test_series_combine_first(scalars_dfs):
16611682
scalars_df, scalars_pandas_df = scalars_dfs

0 commit comments

Comments
 (0)