@@ -2440,36 +2440,47 @@ def test_sort_ascending_list(self):
2440
2440
expected = s .iloc [[0 , 4 , 1 , 5 , 2 , 6 , 3 , 7 ]]
2441
2441
tm .assert_series_equal (result , expected )
2442
2442
2443
+ def test_multiindex_loc_order (self ):
2444
+ # GH 22797
2445
+ # Try to respect order of keys given for MultiIndex.loc
2446
+ df = pd .DataFrame (
2447
+ np .arange (12 ).reshape ((4 , 3 )),
2448
+ index = [["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]],
2449
+ columns = [["Ohio" , "Ohio" , "Colorado" ], ["Green" , "Red" , "Green" ]],
2450
+ )
2443
2451
2444
- def test_multiindex_loc_order ():
2445
- # GH 22797
2446
- # Try to respect order of keys given for MultiIndex.loc
2447
- df = pd .DataFrame (
2448
- np .arange (12 ).reshape ((4 , 3 )),
2449
- index = [["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]],
2450
- columns = [["Ohio" , "Ohio" , "Colorado" ], ["Green" , "Red" , "Green" ]],
2451
- )
2452
+ res = df .loc [["b" , "a" ], :]
2453
+ exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]])
2454
+ tm .assert_index_equal (res .index , exp_index )
2452
2455
2453
- res = df .loc [["b " , "a " ], :]
2454
- exp_index = pd .MultiIndex .from_arrays ([["b " , "b " , "a " , "a " ], [1 , 2 , 1 , 2 ]])
2455
- tm .assert_index_equal (res .index , exp_index )
2456
+ res = df .loc [["a " , "b " ], :]
2457
+ exp_index = pd .MultiIndex .from_arrays ([["a " , "a " , "b " , "b " ], [1 , 2 , 1 , 2 ]])
2458
+ tm .assert_index_equal (res .index , exp_index )
2456
2459
2457
- res = df .loc [["a" , "b" ], :]
2458
- exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]])
2459
- tm .assert_index_equal (res .index , exp_index )
2460
+ res = df .loc [( ["a" , "b" ], [ 1 , 2 ]) , :]
2461
+ exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]])
2462
+ tm .assert_index_equal (res .index , exp_index )
2460
2463
2461
- res = df .loc [(["a" , "b" ], [1 , 2 ]), :]
2462
- exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]])
2463
- tm .assert_index_equal (res .index , exp_index )
2464
+ res = df .loc [(["a" , "b" ], [2 , 1 ]), :]
2465
+ exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [2 , 1 , 2 , 1 ]])
2466
+ tm .assert_index_equal (res .index , exp_index )
2464
2467
2465
- res = df .loc [(["a " , "b " ], [2 , 1 ]), :]
2466
- exp_index = pd .MultiIndex .from_arrays ([["a " , "a " , "b " , "b " ], [2 , 1 , 2 , 1 ]])
2467
- tm .assert_index_equal (res .index , exp_index )
2468
+ res = df .loc [(["b " , "a " ], [2 , 1 ]), :]
2469
+ exp_index = pd .MultiIndex .from_arrays ([["b " , "b " , "a " , "a " ], [2 , 1 , 2 , 1 ]])
2470
+ tm .assert_index_equal (res .index , exp_index )
2468
2471
2469
- res = df .loc [(["b" , "a" ], [2 , 1 ]), :]
2470
- exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [2 , 1 , 2 , 1 ]])
2471
- tm .assert_index_equal (res .index , exp_index )
2472
+ res = df .loc [(["b" , "a" ], [1 , 2 ]), :]
2473
+ exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]])
2474
+ tm .assert_index_equal (res .index , exp_index )
2472
2475
2473
- res = df .loc [(["b" , "a" ], [1 , 2 ]), :]
2474
- exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]])
2475
- tm .assert_index_equal (res .index , exp_index )
2476
+ res = df .loc [:, ["Colorado" , "Ohio" ]]
2477
+ exp_columns = pd .MultiIndex .from_arrays (
2478
+ [["Colorado" , "Ohio" , "Ohio" ], ["Green" , "Green" , "Red" ]]
2479
+ )
2480
+ tm .assert_index_equal (res .columns , exp_columns )
2481
+
2482
+ res = df .loc [:, (["Colorado" , "Ohio" ], ["Red" , "Green" ])]
2483
+ exp_columns = pd .MultiIndex .from_arrays (
2484
+ [["Colorado" , "Ohio" , "Ohio" ], ["Green" , "Red" , "Green" ]]
2485
+ )
2486
+ tm .assert_index_equal (res .columns , exp_columns )
0 commit comments