@@ -6304,6 +6304,47 @@ def test_groupby_categorical_two_columns(self):
6304
6304
nan , nan , nan , nan , 200 , 34 ]}, index = idx )
6305
6305
tm .assert_frame_equal (res , exp )
6306
6306
6307
+ def test_groupby_multi_categorical_as_index (self ):
6308
+ # GH13204
6309
+ df = DataFrame ({'cat' : Categorical ([1 , 2 , 2 ], [1 , 2 , 3 ]),
6310
+ 'A' : [10 , 11 , 11 ],
6311
+ 'B' : [101 , 102 , 103 ]})
6312
+ result = df .groupby (['cat' , 'A' ], as_index = False ).sum ()
6313
+ expected = DataFrame ({'cat' : [1 , 1 , 2 , 2 , 3 , 3 ],
6314
+ 'A' : [10 , 11 , 10 , 11 , 10 , 11 ],
6315
+ 'B' : [101.0 , nan , nan , 205.0 , nan , nan ]},
6316
+ columns = ['cat' , 'A' , 'B' ])
6317
+ tm .assert_frame_equal (result , expected )
6318
+
6319
+ # function grouper
6320
+ f = lambda r : df .loc [r , 'A' ]
6321
+ result = df .groupby (['cat' , f ], as_index = False ).sum ()
6322
+ expected = DataFrame ({'cat' : [1 , 1 , 2 , 2 , 3 , 3 ],
6323
+ 'A' : [10.0 , nan , nan , 22.0 , nan , nan ],
6324
+ 'B' : [101.0 , nan , nan , 205.0 , nan , nan ]},
6325
+ columns = ['cat' , 'A' , 'B' ])
6326
+ tm .assert_frame_equal (result , expected )
6327
+
6328
+ # another not in-axis grouper (conflicting names in index)
6329
+ s = Series (['a' , 'b' , 'b' ], name = 'cat' )
6330
+ result = df .groupby (['cat' , s ], as_index = False ).sum ()
6331
+ expected = DataFrame ({'cat' : [1 , 1 , 2 , 2 , 3 , 3 ],
6332
+ 'A' : [10.0 , nan , nan , 22.0 , nan , nan ],
6333
+ 'B' : [101.0 , nan , nan , 205.0 , nan , nan ]},
6334
+ columns = ['cat' , 'A' , 'B' ])
6335
+ tm .assert_frame_equal (result , expected )
6336
+
6337
+ # is original index dropped?
6338
+ expected = DataFrame ({'cat' : [1 , 1 , 2 , 2 , 3 , 3 ],
6339
+ 'A' : [10 , 11 , 10 , 11 , 10 , 11 ],
6340
+ 'B' : [101.0 , nan , nan , 205.0 , nan , nan ]},
6341
+ columns = ['cat' , 'A' , 'B' ])
6342
+
6343
+ for name in [None , 'X' , 'B' , 'cat' ]:
6344
+ df .index = Index (list ("abc" ), name = name )
6345
+ result = df .groupby (['cat' , 'A' ], as_index = False ).sum ()
6346
+ tm .assert_frame_equal (result , expected , check_index_type = True )
6347
+
6307
6348
def test_groupby_apply_all_none (self ):
6308
6349
# Tests to make sure no errors if apply function returns all None
6309
6350
# values. Issue 9684.
@@ -6431,6 +6472,16 @@ def test_numpy_compat(self):
6431
6472
tm .assertRaisesRegexp (UnsupportedFunctionCall , msg ,
6432
6473
getattr (g , func ), foo = 1 )
6433
6474
6475
+ def test_grouping_string_repr (self ):
6476
+ # GH 13394
6477
+ mi = MultiIndex .from_arrays ([list ("AAB" ), list ("aba" )])
6478
+ df = DataFrame ([[1 , 2 , 3 ]], columns = mi )
6479
+ gr = df .groupby (df [('A' , 'a' )])
6480
+
6481
+ result = gr .grouper .groupings [0 ].__repr__ ()
6482
+ expected = "Grouping(('A', 'a'))"
6483
+ tm .assert_equal (result , expected )
6484
+
6434
6485
6435
6486
def assert_fp_equal (a , b ):
6436
6487
assert (np .abs (a - b ) < 1e-12 ).all ()
0 commit comments