@@ -281,67 +281,93 @@ def test_arg_passthru():
281
281
tm .assert_index_equal (result .columns , expected_columns )
282
282
283
283
284
- def test_non_cython_api ():
285
-
286
- # GH5610
287
- # non-cython calls should not include the grouper
288
-
289
- df = DataFrame (
290
- [[1 , 2 , "foo" ], [1 , np .nan , "bar" ], [3 , np .nan , "baz" ]], columns = ["A" , "B" , "C" ]
291
- )
292
- g = df .groupby ("A" )
293
- gni = df .groupby ("A" , as_index = False )
294
-
295
- # mad
296
- expected = DataFrame ([[0 ], [np .nan ]], columns = ["B" ], index = [1 , 3 ])
297
- expected .index .name = "A"
298
- result = g .mad ()
299
- tm .assert_frame_equal (result , expected )
300
-
301
- expected = DataFrame ([[1 , 0.0 ], [3 , np .nan ]], columns = ["A" , "B" ], index = [0 , 1 ])
302
- result = gni .mad ()
303
- tm .assert_frame_equal (result , expected )
304
-
305
- # describe
306
- expected_index = Index ([1 , 3 ], name = "A" )
307
- expected_col = pd .MultiIndex (
308
- levels = [["B" ], ["count" , "mean" , "std" , "min" , "25%" , "50%" , "75%" , "max" ]],
309
- codes = [[0 ] * 8 , list (range (8 ))],
310
- )
311
- expected = DataFrame (
312
- [
313
- [1.0 , 2.0 , np .nan , 2.0 , 2.0 , 2.0 , 2.0 , 2.0 ],
314
- [0.0 , np .nan , np .nan , np .nan , np .nan , np .nan , np .nan , np .nan ],
315
- ],
316
- index = expected_index ,
317
- columns = expected_col ,
318
- )
319
- result = g .describe ()
320
- tm .assert_frame_equal (result , expected )
321
-
322
- expected = pd .concat (
323
- [
324
- df [df .A == 1 ].describe ().unstack ().to_frame ().T ,
325
- df [df .A == 3 ].describe ().unstack ().to_frame ().T ,
326
- ]
327
- )
328
- expected .index = Index ([0 , 1 ])
329
- result = gni .describe ()
330
- tm .assert_frame_equal (result , expected )
331
-
332
- # any
333
- expected = DataFrame (
334
- [[True , True ], [False , True ]], columns = ["B" , "C" ], index = [1 , 3 ]
335
- )
336
- expected .index .name = "A"
337
- result = g .any ()
338
- tm .assert_frame_equal (result , expected )
284
+ class TestGroupByNonCythonPaths :
285
+ # GH#5610 non-cython calls should not include the grouper
286
+ # Tests for code not expected to go through cython paths.
287
+
288
+ @pytest .fixture
289
+ def df (self ):
290
+ df = DataFrame (
291
+ [[1 , 2 , "foo" ], [1 , np .nan , "bar" ], [3 , np .nan , "baz" ]],
292
+ columns = ["A" , "B" , "C" ],
293
+ )
294
+ return df
295
+
296
+ @pytest .fixture
297
+ def gb (self , df ):
298
+ gb = df .groupby ("A" )
299
+ return gb
300
+
301
+ @pytest .fixture
302
+ def gni (self , df ):
303
+ gni = df .groupby ("A" , as_index = False )
304
+ return gni
305
+
306
+ # TODO: non-unique columns, as_index=False
307
+ def test_idxmax (self , gb ):
308
+ # object dtype so idxmax goes through _aggregate_item_by_item
309
+ # GH#5610
310
+ # non-cython calls should not include the grouper
311
+ expected = DataFrame ([[0.0 ], [np .nan ]], columns = ["B" ], index = [1 , 3 ])
312
+ expected .index .name = "A"
313
+ result = gb .idxmax ()
314
+ tm .assert_frame_equal (result , expected )
315
+
316
+ def test_idxmin (self , gb ):
317
+ # object dtype so idxmax goes through _aggregate_item_by_item
318
+ # GH#5610
319
+ # non-cython calls should not include the grouper
320
+ expected = DataFrame ([[0.0 ], [np .nan ]], columns = ["B" ], index = [1 , 3 ])
321
+ expected .index .name = "A"
322
+ result = gb .idxmin ()
323
+ tm .assert_frame_equal (result , expected )
324
+
325
+ def test_any (self , gb ):
326
+ expected = DataFrame (
327
+ [[True , True ], [False , True ]], columns = ["B" , "C" ], index = [1 , 3 ]
328
+ )
329
+ expected .index .name = "A"
330
+ result = gb .any ()
331
+ tm .assert_frame_equal (result , expected )
332
+
333
+ def test_mad (self , gb , gni ):
334
+ # mad
335
+ expected = DataFrame ([[0 ], [np .nan ]], columns = ["B" ], index = [1 , 3 ])
336
+ expected .index .name = "A"
337
+ result = gb .mad ()
338
+ tm .assert_frame_equal (result , expected )
339
+
340
+ expected = DataFrame ([[1 , 0.0 ], [3 , np .nan ]], columns = ["A" , "B" ], index = [0 , 1 ])
341
+ result = gni .mad ()
342
+ tm .assert_frame_equal (result , expected )
343
+
344
+ def test_describe (self , df , gb , gni ):
345
+ # describe
346
+ expected_index = Index ([1 , 3 ], name = "A" )
347
+ expected_col = pd .MultiIndex (
348
+ levels = [["B" ], ["count" , "mean" , "std" , "min" , "25%" , "50%" , "75%" , "max" ]],
349
+ codes = [[0 ] * 8 , list (range (8 ))],
350
+ )
351
+ expected = DataFrame (
352
+ [
353
+ [1.0 , 2.0 , np .nan , 2.0 , 2.0 , 2.0 , 2.0 , 2.0 ],
354
+ [0.0 , np .nan , np .nan , np .nan , np .nan , np .nan , np .nan , np .nan ],
355
+ ],
356
+ index = expected_index ,
357
+ columns = expected_col ,
358
+ )
359
+ result = gb .describe ()
360
+ tm .assert_frame_equal (result , expected )
339
361
340
- # idxmax
341
- expected = DataFrame ([[0.0 ], [np .nan ]], columns = ["B" ], index = [1 , 3 ])
342
- expected .index .name = "A"
343
- result = g .idxmax ()
344
- tm .assert_frame_equal (result , expected )
362
+ expected = pd .concat (
363
+ [
364
+ df [df .A == 1 ].describe ().unstack ().to_frame ().T ,
365
+ df [df .A == 3 ].describe ().unstack ().to_frame ().T ,
366
+ ]
367
+ )
368
+ expected .index = Index ([0 , 1 ])
369
+ result = gni .describe ()
370
+ tm .assert_frame_equal (result , expected )
345
371
346
372
347
373
def test_cython_api2 ():
0 commit comments