This repository was archived by the owner on Apr 1, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2926,9 +2926,23 @@ def nunique(self) -> bigframes.series.Series:
29262926 return bigframes .series .Series (block )
29272927
29282928 def agg (
2929- self , func : str | typing .Sequence [str ]
2929+ self ,
2930+ func : str
2931+ | typing .Sequence [str ]
2932+ | typing .Mapping [blocks .Label , typing .Sequence [str ] | str ],
29302933 ) -> DataFrame | bigframes .series .Series :
2931- if utils .is_list_like (func ):
2934+ if utils .is_dict_like (func ):
2935+ # Must check dict-like first because dictionaries are list-like
2936+ # according to Pandas.
2937+ agg_cols = []
2938+ for col_label , agg_func in func .items ():
2939+ agg_cols .append (self [col_label ].agg (agg_func ))
2940+
2941+ from bigframes .core .reshape import api as reshape
2942+
2943+ return reshape .concat (agg_cols , axis = 1 )
2944+
2945+ elif utils .is_list_like (func ):
29322946 aggregations = [agg_ops .lookup_agg_func (f ) for f in func ]
29332947
29342948 for dtype , agg in itertools .product (self .dtypes , aggregations ):
@@ -2942,6 +2956,7 @@ def agg(
29422956 aggregations ,
29432957 )
29442958 )
2959+
29452960 else :
29462961 return bigframes .series .Series (
29472962 self ._block .aggregate_all_and_stack (
Original file line number Diff line number Diff line change @@ -5652,3 +5652,29 @@ def test_astype_invalid_type_fail(scalars_dfs):
56525652
56535653 with pytest .raises (TypeError , match = r".*Share your usecase with.*" ):
56545654 bf_df .astype (123 )
5655+
5656+
5657+ def test_agg_with_dict (scalars_dfs ):
5658+ bf_df , pd_df = scalars_dfs
5659+ agg_funcs = {
5660+ "int64_too" : ["min" , "max" ],
5661+ "int64_col" : ["min" , "count" ],
5662+ }
5663+
5664+ bf_result = bf_df .agg (agg_funcs ).to_pandas ()
5665+ pd_result = pd_df .agg (agg_funcs )
5666+
5667+ pd .testing .assert_frame_equal (
5668+ bf_result , pd_result , check_dtype = False , check_index_type = False
5669+ )
5670+
5671+
5672+ def test_agg_with_dict_containing_non_existing_col_raise_key_error (scalars_dfs ):
5673+ bf_df , _ = scalars_dfs
5674+ agg_funcs = {
5675+ "int64_too" : ["min" , "max" ],
5676+ "nonexisting_col" : ["count" ],
5677+ }
5678+
5679+ with pytest .raises (KeyError ):
5680+ bf_df .agg (agg_funcs )
You can’t perform that action at this time.
0 commit comments