@@ -253,158 +253,6 @@ def test_grouper_column_and_index(self):
253
253
expected = df_single .reset_index ().groupby (['inner' , 'B' ]).mean ()
254
254
assert_frame_equal (result , expected )
255
255
256
- def test_grouper_index_level_as_string (self ):
257
- # GH 5677, allow strings passed as the `by` parameter to reference
258
- # columns or index levels
259
-
260
- idx = pd .MultiIndex .from_tuples ([('a' , 1 ), ('a' , 2 ), ('a' , 3 ),
261
- ('b' , 1 ), ('b' , 2 ), ('b' , 3 )])
262
- idx .names = ['outer' , 'inner' ]
263
- df_multi = pd .DataFrame ({"A" : np .arange (6 ),
264
- 'B' : ['one' , 'one' , 'two' ,
265
- 'two' , 'one' , 'one' ]},
266
- index = idx )
267
-
268
- df_single = df_multi .reset_index ('outer' )
269
-
270
- # Column and Index on MultiIndex
271
- result = df_multi .groupby (['B' , 'inner' ]).mean ()
272
- expected = df_multi .groupby (['B' , pd .Grouper (level = 'inner' )]).mean ()
273
- assert_frame_equal (result , expected )
274
-
275
- # Index and Column on MultiIndex
276
- result = df_multi .groupby (['inner' , 'B' ]).mean ()
277
- expected = df_multi .groupby ([pd .Grouper (level = 'inner' ), 'B' ]).mean ()
278
- assert_frame_equal (result , expected )
279
-
280
- # Column and Index on single Index
281
- result = df_single .groupby (['B' , 'inner' ]).mean ()
282
- expected = df_single .groupby (['B' , pd .Grouper (level = 'inner' )]).mean ()
283
- assert_frame_equal (result , expected )
284
-
285
- # Index and Column on single Index
286
- result = df_single .groupby (['inner' , 'B' ]).mean ()
287
- expected = df_single .groupby ([pd .Grouper (level = 'inner' ), 'B' ]).mean ()
288
- assert_frame_equal (result , expected )
289
-
290
- # Single element list of Index on MultiIndex
291
- result = df_multi .groupby (['inner' ]).mean ()
292
- expected = df_multi .groupby (pd .Grouper (level = 'inner' )).mean ()
293
- assert_frame_equal (result , expected )
294
-
295
- # Single element list of Index on single Index
296
- result = df_single .groupby (['inner' ]).mean ()
297
- expected = df_single .groupby (pd .Grouper (level = 'inner' )).mean ()
298
- assert_frame_equal (result , expected )
299
-
300
- # Index on MultiIndex
301
- result = df_multi .groupby ('inner' ).mean ()
302
- expected = df_multi .groupby (pd .Grouper (level = 'inner' )).mean ()
303
- assert_frame_equal (result , expected )
304
-
305
- # Index on single Index
306
- result = df_single .groupby ('inner' ).mean ()
307
- expected = df_single .groupby (pd .Grouper (level = 'inner' )).mean ()
308
- assert_frame_equal (result , expected )
309
-
310
- def test_grouper_column_index_level_precedence (self ):
311
- # GH 5677, when a string passed as the `by` parameter
312
- # matches a column and an index level the column takes
313
- # precedence
314
-
315
- idx = pd .MultiIndex .from_tuples ([('a' , 1 ), ('a' , 2 ), ('a' , 3 ),
316
- ('b' , 1 ), ('b' , 2 ), ('b' , 3 )])
317
- idx .names = ['outer' , 'inner' ]
318
- df_multi_both = pd .DataFrame ({"A" : np .arange (6 ),
319
- 'B' : ['one' , 'one' , 'two' ,
320
- 'two' , 'one' , 'one' ],
321
- 'inner' : [1 , 1 , 1 , 1 , 1 , 1 ]},
322
- index = idx )
323
-
324
- df_single_both = df_multi_both .reset_index ('outer' )
325
-
326
- # Group MultiIndex by single key
327
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
328
- result = df_multi_both .groupby ('inner' ).mean ()
329
-
330
- expected = df_multi_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
331
- assert_frame_equal (result , expected )
332
- not_expected = df_multi_both .groupby (pd .Grouper (level = 'inner' )).mean ()
333
- assert not result .index .equals (not_expected .index )
334
-
335
- # Group single Index by single key
336
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
337
- result = df_single_both .groupby ('inner' ).mean ()
338
-
339
- expected = df_single_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
340
- assert_frame_equal (result , expected )
341
- not_expected = df_single_both .groupby (pd .Grouper (level = 'inner' )).mean ()
342
- assert not result .index .equals (not_expected .index )
343
-
344
- # Group MultiIndex by single key list
345
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
346
- result = df_multi_both .groupby (['inner' ]).mean ()
347
-
348
- expected = df_multi_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
349
- assert_frame_equal (result , expected )
350
- not_expected = df_multi_both .groupby (pd .Grouper (level = 'inner' )).mean ()
351
- assert not result .index .equals (not_expected .index )
352
-
353
- # Group single Index by single key list
354
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
355
- result = df_single_both .groupby (['inner' ]).mean ()
356
-
357
- expected = df_single_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
358
- assert_frame_equal (result , expected )
359
- not_expected = df_single_both .groupby (pd .Grouper (level = 'inner' )).mean ()
360
- assert not result .index .equals (not_expected .index )
361
-
362
- # Group MultiIndex by two keys (1)
363
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
364
- result = df_multi_both .groupby (['B' , 'inner' ]).mean ()
365
-
366
- expected = df_multi_both .groupby (['B' ,
367
- pd .Grouper (key = 'inner' )]).mean ()
368
- assert_frame_equal (result , expected )
369
- not_expected = df_multi_both .groupby (['B' ,
370
- pd .Grouper (level = 'inner' )
371
- ]).mean ()
372
- assert not result .index .equals (not_expected .index )
373
-
374
- # Group MultiIndex by two keys (2)
375
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
376
- result = df_multi_both .groupby (['inner' , 'B' ]).mean ()
377
-
378
- expected = df_multi_both .groupby ([pd .Grouper (key = 'inner' ),
379
- 'B' ]).mean ()
380
- assert_frame_equal (result , expected )
381
- not_expected = df_multi_both .groupby ([pd .Grouper (level = 'inner' ),
382
- 'B' ]).mean ()
383
- assert not result .index .equals (not_expected .index )
384
-
385
- # Group single Index by two keys (1)
386
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
387
- result = df_single_both .groupby (['B' , 'inner' ]).mean ()
388
-
389
- expected = df_single_both .groupby (['B' ,
390
- pd .Grouper (key = 'inner' )]).mean ()
391
- assert_frame_equal (result , expected )
392
- not_expected = df_single_both .groupby (['B' ,
393
- pd .Grouper (level = 'inner' )
394
- ]).mean ()
395
- assert not result .index .equals (not_expected .index )
396
-
397
- # Group single Index by two keys (2)
398
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
399
- result = df_single_both .groupby (['inner' , 'B' ]).mean ()
400
-
401
- expected = df_single_both .groupby ([pd .Grouper (key = 'inner' ),
402
- 'B' ]).mean ()
403
- assert_frame_equal (result , expected )
404
- not_expected = df_single_both .groupby ([pd .Grouper (level = 'inner' ),
405
- 'B' ]).mean ()
406
- assert not result .index .equals (not_expected .index )
407
-
408
256
def test_grouper_getting_correct_binner (self ):
409
257
410
258
# GH 10063
0 commit comments