@@ -1057,26 +1057,65 @@ def test_non_callable_aggregates(self):
1057
1057
1058
1058
assert result == expected
1059
1059
1060
- @pytest .mark .parametrize ("df" , [
1061
- pd .DataFrame ([[1 , 2 ], [3 , 4 ]]),
1062
- pd .DataFrame ([[np .nan , 2 ], [3 , 4 ]]),
1063
- pd .DataFrame (),
1060
+ @pytest .mark .parametrize ("inputs" , [
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
+ }],
1064
1103
])
1065
- def test_agg_function_input (self , df , cython_table_items ):
1066
- # test whether the functions (keys) in
1067
- # pd.core.base.SelectionMixin._cython_table give the same result
1068
- # as the related strings (values) when used in df.agg. Examples:
1069
- # - ``df.agg(np.nansum)`` should give the same result as
1070
- # ``df.agg('sum')``
1071
- # - ``df.agg(sum)`` should give the same result as ``df.agg('sum')``
1072
- # etc.
1104
+ @pytest .mark .parametrize ("axis" , [0 , 1 ], ids = lambda x : "axis {}" .format (x ))
1105
+ def test_agg_function_input (self , cython_table_items , inputs , axis ):
1073
1106
# GH21123
1074
1107
np_func , str_func = cython_table_items
1075
- if str_func in ('cumprod' , 'cumsum' ):
1076
- tm .assert_frame_equal (df .agg (np_func ), df .agg (str_func ))
1077
- tm .assert_frame_equal (df .agg (np_func , axis = 1 ),
1078
- df .agg (str_func , axis = 1 ))
1108
+ df = inputs [0 ]
1109
+ expected = inputs [1 ][str_func ]
1110
+
1111
+ if isinstance (expected , type ) and issubclass (expected , Exception ):
1112
+ with pytest .raises (expected ):
1113
+ # e.g. DataFrame(['a b'.split()]).cumprod() will raise
1114
+ df .agg (np_func , axis = axis )
1115
+ df .agg (str_func , axis = axis )
1116
+ elif str_func in ('cumprod' , 'cumsum' ):
1117
+ tm .assert_frame_equal (df .agg (np_func , axis = axis ), expected )
1118
+ tm .assert_frame_equal (df .agg (str_func , axis = axis ), expected )
1079
1119
else :
1080
- tm .assert_series_equal (df .agg (np_func ), df .agg (str_func ))
1081
- tm .assert_series_equal (df .agg (np_func , axis = 1 ),
1082
- df .agg (str_func , axis = 1 ))
1120
+ tm .assert_series_equal (df .agg (np_func , axis = axis ), expected )
1121
+ tm .assert_series_equal (df .agg (str_func , axis = axis ), expected )
0 commit comments