@@ -946,7 +946,7 @@ def test_agg_dict_nested_renaming_depr(self):
946
946
df = pd .DataFrame ({'A' : range (5 ), 'B' : 5 })
947
947
948
948
# nested renaming
949
- with tm .assert_produces_warning (FutureWarning ):
949
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
950
950
df .agg ({'A' : {'foo' : 'min' },
951
951
'B' : {'bar' : 'max' }})
952
952
@@ -1056,3 +1056,73 @@ def test_non_callable_aggregates(self):
1056
1056
expected = df .size
1057
1057
1058
1058
assert result == expected
1059
+
1060
+ @pytest .mark .parametrize ("frame, expected_dict" , [
1061
+ [DataFrame (), {
1062
+ 'sum' : Series (),
1063
+ 'max' : Series (),
1064
+ 'min' : Series (),
1065
+ 'all' : Series (dtype = bool ),
1066
+ 'any' : Series (dtype = bool ),
1067
+ 'mean' : Series (),
1068
+ 'prod' : Series (),
1069
+ 'std' : Series (),
1070
+ 'var' : Series (),
1071
+ 'median' : Series (),
1072
+ 'cumprod' : DataFrame (),
1073
+ 'cumsum' : DataFrame (),
1074
+ }],
1075
+ [DataFrame ([[np .nan , 1 ], [1 , 2 ]]), {
1076
+ 'sum' : Series ([1. , 3 ]),
1077
+ 'max' : Series ([1. , 2 ]),
1078
+ 'min' : Series ([1. , 1 ]),
1079
+ 'all' : Series ([True , True ]),
1080
+ 'any' : Series ([True , True ]),
1081
+ 'mean' : Series ([1 , 1.5 ]),
1082
+ 'prod' : Series ([1. , 2 ]),
1083
+ 'std' : Series ([np .nan , 0.707107 ]),
1084
+ 'var' : Series ([np .nan , 0.5 ]),
1085
+ 'median' : Series ([1 , 1.5 ]),
1086
+ 'cumprod' : DataFrame ([[np .nan , 1 ], [1. , 2. ]]),
1087
+ 'cumsum' : DataFrame ([[np .nan , 1 ], [1. , 3. ]]),
1088
+ }],
1089
+ [DataFrame ([['a' , 'b' ], ['b' , 'a' ]]), {
1090
+ 'sum' : Series (['ab' , 'ba' ]),
1091
+ 'max' : Series (['b' , 'b' ]),
1092
+ 'min' : Series (['a' , 'a' ]),
1093
+ 'all' : Series ([True , True ]),
1094
+ 'any' : Series ([True , True ]),
1095
+ 'mean' : Series ([], index = pd .Index ([], dtype = 'int64' )),
1096
+ 'prod' : Series ([], index = pd .Index ([], dtype = 'int64' )),
1097
+ 'std' : Series ([], index = pd .Index ([], dtype = 'int64' )),
1098
+ 'var' : Series ([], index = pd .Index ([], dtype = 'int64' )),
1099
+ 'median' : Series ([], index = pd .Index ([], dtype = 'int64' )),
1100
+ 'cumprod' : TypeError ,
1101
+ 'cumsum' : DataFrame ([['a' , 'b' ], ['ab' , 'ba' ]]),
1102
+ }],
1103
+ ])
1104
+ @pytest .mark .parametrize ("axis" , [0 , 1 ], ids = lambda x : "axis {}" .format (x ))
1105
+ def test_agg_cython_table (self , cython_table_items ,
1106
+ frame , expected_dict , axis ):
1107
+ # GH21222
1108
+ # test if using items in pandas.core.base.SelectionMixin._cython_table
1109
+ # in agg gives correct results
1110
+ np_func , str_func = cython_table_items
1111
+ expected = expected_dict [str_func ]
1112
+
1113
+ if isinstance (expected , type ) and issubclass (expected , Exception ):
1114
+ with pytest .raises (expected ):
1115
+ # e.g. DataFrame(['a b'.split()]).cumprod() will raise
1116
+ frame .agg (np_func , axis = axis )
1117
+ with pytest .raises (expected ):
1118
+ frame .agg (str_func , axis = axis )
1119
+ return
1120
+
1121
+ result = frame .agg (np_func , axis = axis )
1122
+ result_str_func = frame .agg (str_func , axis = axis )
1123
+ if str_func in ('cumprod' , 'cumsum' ):
1124
+ tm .assert_frame_equal (result , expected )
1125
+ tm .assert_frame_equal (result_str_func , expected )
1126
+ else :
1127
+ tm .assert_series_equal (result , expected )
1128
+ tm .assert_series_equal (result_str_func , expected )
0 commit comments