@@ -637,7 +637,7 @@ def test_agg_consistency(self):
637
637
def P1 (a ):
638
638
try :
639
639
return np .percentile (a .dropna (), q = 1 )
640
- except :
640
+ except Exception :
641
641
return np .nan
642
642
643
643
import datetime as dt
@@ -892,3 +892,36 @@ def test_sum_uint64_overflow(self):
892
892
expected .index .name = 0
893
893
result = df .groupby (0 ).sum ()
894
894
tm .assert_frame_equal (result , expected )
895
+
896
+ @pytest .mark .parametrize ("structure, expected" , [
897
+ (tuple , pd .DataFrame ({'C' : {(1 , 1 ): (1 , 1 , 1 ), (3 , 4 ): (3 , 4 , 4 )}})),
898
+ (list , pd .DataFrame ({'C' : {(1 , 1 ): [1 , 1 , 1 ], (3 , 4 ): [3 , 4 , 4 ]}})),
899
+ (lambda x : tuple (x ), pd .DataFrame ({'C' : {(1 , 1 ): (1 , 1 , 1 ),
900
+ (3 , 4 ): (3 , 4 , 4 )}})),
901
+ (lambda x : list (x ), pd .DataFrame ({'C' : {(1 , 1 ): [1 , 1 , 1 ],
902
+ (3 , 4 ): [3 , 4 , 4 ]}}))
903
+ ])
904
+ def test_agg_structs_dataframe (self , structure , expected ):
905
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 3 , 3 , 3 ],
906
+ 'B' : [1 , 1 , 1 , 4 , 4 , 4 ], 'C' : [1 , 1 , 1 , 3 , 4 , 4 ]})
907
+
908
+ result = df .groupby (['A' , 'B' ]).aggregate (structure )
909
+ expected .index .names = ['A' , 'B' ]
910
+ assert_frame_equal (result , expected )
911
+
912
+ @pytest .mark .parametrize ("structure, expected" , [
913
+ (tuple , pd .Series ([(1 , 1 , 1 ), (3 , 4 , 4 )], index = [1 , 3 ], name = 'C' )),
914
+ (list , pd .Series ([[1 , 1 , 1 ], [3 , 4 , 4 ]], index = [1 , 3 ], name = 'C' )),
915
+ (lambda x : tuple (x ), pd .Series ([(1 , 1 , 1 ), (3 , 4 , 4 )],
916
+ index = [1 , 3 ], name = 'C' )),
917
+ (lambda x : list (x ), pd .Series ([[1 , 1 , 1 ], [3 , 4 , 4 ]],
918
+ index = [1 , 3 ], name = 'C' ))
919
+ ])
920
+ def test_agg_structs_series (self , structure , expected ):
921
+ # Issue #18079
922
+ df = pd .DataFrame ({'A' : [1 , 1 , 1 , 3 , 3 , 3 ],
923
+ 'B' : [1 , 1 , 1 , 4 , 4 , 4 ], 'C' : [1 , 1 , 1 , 3 , 4 , 4 ]})
924
+
925
+ result = df .groupby ('A' )['C' ].aggregate (structure )
926
+ expected .index .name = 'A'
927
+ assert_series_equal (result , expected )
0 commit comments