11"""
22test .agg behavior / note that .apply is tested generally in test_groupby.py
33"""
4- from collections import OrderedDict
54import functools
65
76import numpy as np
@@ -175,18 +174,14 @@ def test_aggregate_str_func(tsframe, groupbyfunc):
175174 tm .assert_frame_equal (result , expected )
176175
177176 # group frame by function dict
178- result = grouped .agg (
179- OrderedDict ([["A" , "var" ], ["B" , "std" ], ["C" , "mean" ], ["D" , "sem" ]])
180- )
177+ result = grouped .agg ({"A" : "var" , "B" : "std" , "C" : "mean" , "D" : "sem" })
181178 expected = DataFrame (
182- OrderedDict (
183- [
184- ["A" , grouped ["A" ].var ()],
185- ["B" , grouped ["B" ].std ()],
186- ["C" , grouped ["C" ].mean ()],
187- ["D" , grouped ["D" ].sem ()],
188- ]
189- )
179+ {
180+ "A" : grouped ["A" ].var (),
181+ "B" : grouped ["B" ].std (),
182+ "C" : grouped ["C" ].mean (),
183+ "D" : grouped ["D" ].sem (),
184+ }
190185 )
191186 tm .assert_frame_equal (result , expected )
192187
@@ -261,22 +256,20 @@ def test_multiple_functions_tuples_and_non_tuples(df):
261256def test_more_flexible_frame_multi_function (df ):
262257 grouped = df .groupby ("A" )
263258
264- exmean = grouped .agg (OrderedDict ([[ "C" , np .mean ], [ "D" , np .mean ]]) )
265- exstd = grouped .agg (OrderedDict ([[ "C" , np .std ], [ "D" , np .std ]]) )
259+ exmean = grouped .agg ({ "C" : np .mean , "D" : np .mean } )
260+ exstd = grouped .agg ({ "C" : np .std , "D" : np .std } )
266261
267262 expected = concat ([exmean , exstd ], keys = ["mean" , "std" ], axis = 1 )
268263 expected = expected .swaplevel (0 , 1 , axis = 1 ).sort_index (level = 0 , axis = 1 )
269264
270- d = OrderedDict ([[ "C" , [np .mean , np .std ]], [ "D" , [np .mean , np .std ]]])
265+ d = { "C" : [np .mean , np .std ], "D" : [np .mean , np .std ]}
271266 result = grouped .aggregate (d )
272267
273268 tm .assert_frame_equal (result , expected )
274269
275270 # be careful
276- result = grouped .aggregate (OrderedDict ([["C" , np .mean ], ["D" , [np .mean , np .std ]]]))
277- expected = grouped .aggregate (
278- OrderedDict ([["C" , np .mean ], ["D" , [np .mean , np .std ]]])
279- )
271+ result = grouped .aggregate ({"C" : np .mean , "D" : [np .mean , np .std ]})
272+ expected = grouped .aggregate ({"C" : np .mean , "D" : [np .mean , np .std ]})
280273 tm .assert_frame_equal (result , expected )
281274
282275 def foo (x ):
@@ -288,13 +281,11 @@ def bar(x):
288281 # this uses column selection & renaming
289282 msg = r"nested renamer is not supported"
290283 with pytest .raises (SpecificationError , match = msg ):
291- d = OrderedDict (
292- [["C" , np .mean ], ["D" , OrderedDict ([["foo" , np .mean ], ["bar" , np .std ]])]]
293- )
284+ d = dict ([["C" , np .mean ], ["D" , dict ([["foo" , np .mean ], ["bar" , np .std ]])]])
294285 grouped .aggregate (d )
295286
296287 # But without renaming, these functions are OK
297- d = OrderedDict ([[ "C" , [np .mean ]], [ "D" , [foo , bar ]]])
288+ d = { "C" : [np .mean ], "D" : [foo , bar ]}
298289 grouped .aggregate (d )
299290
300291
@@ -303,26 +294,20 @@ def test_multi_function_flexible_mix(df):
303294 grouped = df .groupby ("A" )
304295
305296 # Expected
306- d = OrderedDict (
307- [["C" , OrderedDict ([["foo" , "mean" ], ["bar" , "std" ]])], ["D" , {"sum" : "sum" }]]
308- )
297+ d = {"C" : {"foo" : "mean" , "bar" : "std" }, "D" : {"sum" : "sum" }}
309298 # this uses column selection & renaming
310299 msg = r"nested renamer is not supported"
311300 with pytest .raises (SpecificationError , match = msg ):
312301 grouped .aggregate (d )
313302
314303 # Test 1
315- d = OrderedDict (
316- [["C" , OrderedDict ([["foo" , "mean" ], ["bar" , "std" ]])], ["D" , "sum" ]]
317- )
304+ d = {"C" : {"foo" : "mean" , "bar" : "std" }, "D" : "sum" }
318305 # this uses column selection & renaming
319306 with pytest .raises (SpecificationError , match = msg ):
320307 grouped .aggregate (d )
321308
322309 # Test 2
323- d = OrderedDict (
324- [["C" , OrderedDict ([["foo" , "mean" ], ["bar" , "std" ]])], ["D" , ["sum" ]]]
325- )
310+ d = {"C" : {"foo" : "mean" , "bar" : "std" }, "D" : "sum" }
326311 # this uses column selection & renaming
327312 with pytest .raises (SpecificationError , match = msg ):
328313 grouped .aggregate (d )
@@ -642,9 +627,7 @@ def test_maybe_mangle_lambdas_args(self):
642627 assert func ["A" ][0 ](0 , 2 , b = 3 ) == (0 , 2 , 3 )
643628
644629 def test_maybe_mangle_lambdas_named (self ):
645- func = OrderedDict (
646- [("C" , np .mean ), ("D" , OrderedDict ([("foo" , np .mean ), ("bar" , np .mean )]))]
647- )
630+ func = {"C" : np .mean , "D" : {"foo" : np .mean , "bar" : np .mean }}
648631 result = _maybe_mangle_lambdas (func )
649632 assert result == func
650633
0 commit comments