Skip to content

Commit 0f047b4

Browse files
fix: DataFrameGroupby.agg now works with unnamed tuples (#985)
1 parent 30e11d9 commit 0f047b4

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

bigframes/core/groupby/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,10 @@ def _agg_named(self, **kwargs) -> df.DataFrame:
414414
raise NotImplementedError(
415415
f"Only string aggregate names supported. {constants.FEEDBACK_LINK}"
416416
)
417-
if not hasattr(v, "column") or not hasattr(v, "aggfunc"):
418-
import bigframes.pandas as bpd
419-
420-
raise TypeError(f"kwargs values must be {bpd.NamedAgg.__qualname__}")
421-
col_id = self._resolve_label(v.column)
422-
aggregations.append((col_id, agg_ops.lookup_agg_func(v.aggfunc)))
417+
if not isinstance(v, tuple) or (len(v) != 2):
418+
raise TypeError("kwargs values must be 2-tuples of column, aggfunc")
419+
col_id = self._resolve_label(v[0])
420+
aggregations.append((col_id, agg_ops.lookup_agg_func(v[1])))
423421
column_labels.append(k)
424422
agg_block, _ = self._block.aggregate(
425423
by_column_ids=self._by_col_ids,

tests/system/small/test_groupby.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,41 @@ def test_dataframe_groupby_agg_named(scalars_df_index, scalars_pandas_df_index):
247247
pd.testing.assert_frame_equal(pd_result, bf_result_computed, check_dtype=False)
248248

249249

250+
def test_dataframe_groupby_agg_kw_tuples(scalars_df_index, scalars_pandas_df_index):
251+
col_names = ["int64_too", "float64_col", "int64_col", "bool_col", "string_col"]
252+
bf_result = (
253+
scalars_df_index[col_names]
254+
.groupby("string_col")
255+
.agg(
256+
agg1=("int64_too", "sum"),
257+
agg2=("float64_col", "max"),
258+
)
259+
)
260+
pd_result = (
261+
scalars_pandas_df_index[col_names]
262+
.groupby("string_col")
263+
.agg(agg1=("int64_too", "sum"), agg2=("float64_col", "max"))
264+
)
265+
bf_result_computed = bf_result.to_pandas()
266+
267+
pd.testing.assert_frame_equal(pd_result, bf_result_computed, check_dtype=False)
268+
269+
270+
@pytest.mark.parametrize(
271+
("kwargs"),
272+
[
273+
({"hello": "world"}),
274+
({"too_many_fields": ("one", "two", "three")}),
275+
],
276+
)
277+
def test_dataframe_groupby_agg_kw_error(scalars_df_index, kwargs):
278+
col_names = ["int64_too", "float64_col", "int64_col", "bool_col", "string_col"]
279+
with pytest.raises(
280+
TypeError, match=r"kwargs values must be 2-tuples of column, aggfunc"
281+
):
282+
(scalars_df_index[col_names].groupby("string_col").agg(**kwargs))
283+
284+
250285
@pytest.mark.parametrize(
251286
("as_index"),
252287
[

0 commit comments

Comments
 (0)