1
1
"""
2
2
test .agg behavior / note that .apply is tested generally in test_groupby.py
3
3
"""
4
- from collections import OrderedDict
5
4
import functools
6
5
7
6
import numpy as np
@@ -175,18 +174,14 @@ def test_aggregate_str_func(tsframe, groupbyfunc):
175
174
tm .assert_frame_equal (result , expected )
176
175
177
176
# 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" })
181
178
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
+ }
190
185
)
191
186
tm .assert_frame_equal (result , expected )
192
187
@@ -261,22 +256,20 @@ def test_multiple_functions_tuples_and_non_tuples(df):
261
256
def test_more_flexible_frame_multi_function (df ):
262
257
grouped = df .groupby ("A" )
263
258
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 } )
266
261
267
262
expected = concat ([exmean , exstd ], keys = ["mean" , "std" ], axis = 1 )
268
263
expected = expected .swaplevel (0 , 1 , axis = 1 ).sort_index (level = 0 , axis = 1 )
269
264
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 ]}
271
266
result = grouped .aggregate (d )
272
267
273
268
tm .assert_frame_equal (result , expected )
274
269
275
270
# 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 ]})
280
273
tm .assert_frame_equal (result , expected )
281
274
282
275
def foo (x ):
@@ -288,13 +281,11 @@ def bar(x):
288
281
# this uses column selection & renaming
289
282
msg = r"nested renamer is not supported"
290
283
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 ]])]])
294
285
grouped .aggregate (d )
295
286
296
287
# But without renaming, these functions are OK
297
- d = OrderedDict ([[ "C" , [np .mean ]], [ "D" , [foo , bar ]]])
288
+ d = { "C" : [np .mean ], "D" : [foo , bar ]}
298
289
grouped .aggregate (d )
299
290
300
291
@@ -303,26 +294,20 @@ def test_multi_function_flexible_mix(df):
303
294
grouped = df .groupby ("A" )
304
295
305
296
# 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" }}
309
298
# this uses column selection & renaming
310
299
msg = r"nested renamer is not supported"
311
300
with pytest .raises (SpecificationError , match = msg ):
312
301
grouped .aggregate (d )
313
302
314
303
# Test 1
315
- d = OrderedDict (
316
- [["C" , OrderedDict ([["foo" , "mean" ], ["bar" , "std" ]])], ["D" , "sum" ]]
317
- )
304
+ d = {"C" : {"foo" : "mean" , "bar" : "std" }, "D" : "sum" }
318
305
# this uses column selection & renaming
319
306
with pytest .raises (SpecificationError , match = msg ):
320
307
grouped .aggregate (d )
321
308
322
309
# Test 2
323
- d = OrderedDict (
324
- [["C" , OrderedDict ([["foo" , "mean" ], ["bar" , "std" ]])], ["D" , ["sum" ]]]
325
- )
310
+ d = {"C" : {"foo" : "mean" , "bar" : "std" }, "D" : "sum" }
326
311
# this uses column selection & renaming
327
312
with pytest .raises (SpecificationError , match = msg ):
328
313
grouped .aggregate (d )
@@ -642,9 +627,7 @@ def test_maybe_mangle_lambdas_args(self):
642
627
assert func ["A" ][0 ](0 , 2 , b = 3 ) == (0 , 2 , 3 )
643
628
644
629
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 }}
648
631
result = _maybe_mangle_lambdas (func )
649
632
assert result == func
650
633
0 commit comments