@@ -2535,36 +2535,47 @@ def test_sort_ascending_list(self):
2535
2535
expected = s .iloc [[0 , 4 , 1 , 5 , 2 , 6 , 3 , 7 ]]
2536
2536
tm .assert_series_equal (result , expected )
2537
2537
2538
+ def test_multiindex_loc_order (self ):
2539
+ # GH 22797
2540
+ # Try to respect order of keys given for MultiIndex.loc
2541
+ df = pd .DataFrame (
2542
+ np .arange (12 ).reshape ((4 , 3 )),
2543
+ index = [["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]],
2544
+ columns = [["Ohio" , "Ohio" , "Colorado" ], ["Green" , "Red" , "Green" ]],
2545
+ )
2538
2546
2539
- def test_multiindex_loc_order ():
2540
- # GH 22797
2541
- # Try to respect order of keys given for MultiIndex.loc
2542
- df = pd .DataFrame (
2543
- np .arange (12 ).reshape ((4 , 3 )),
2544
- index = [["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]],
2545
- columns = [["Ohio" , "Ohio" , "Colorado" ], ["Green" , "Red" , "Green" ]],
2546
- )
2547
+ res = df .loc [["b" , "a" ], :]
2548
+ exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]])
2549
+ tm .assert_index_equal (res .index , exp_index )
2547
2550
2548
- res = df .loc [["b " , "a " ], :]
2549
- exp_index = pd .MultiIndex .from_arrays ([["b " , "b " , "a " , "a " ], [1 , 2 , 1 , 2 ]])
2550
- tm .assert_index_equal (res .index , exp_index )
2551
+ res = df .loc [["a " , "b " ], :]
2552
+ exp_index = pd .MultiIndex .from_arrays ([["a " , "a " , "b " , "b " ], [1 , 2 , 1 , 2 ]])
2553
+ tm .assert_index_equal (res .index , exp_index )
2551
2554
2552
- res = df .loc [["a" , "b" ], :]
2553
- exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]])
2554
- tm .assert_index_equal (res .index , exp_index )
2555
+ res = df .loc [( ["a" , "b" ], [ 1 , 2 ]) , :]
2556
+ exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]])
2557
+ tm .assert_index_equal (res .index , exp_index )
2555
2558
2556
- res = df .loc [(["a" , "b" ], [1 , 2 ]), :]
2557
- exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]])
2558
- tm .assert_index_equal (res .index , exp_index )
2559
+ res = df .loc [(["a" , "b" ], [2 , 1 ]), :]
2560
+ exp_index = pd .MultiIndex .from_arrays ([["a" , "a" , "b" , "b" ], [2 , 1 , 2 , 1 ]])
2561
+ tm .assert_index_equal (res .index , exp_index )
2559
2562
2560
- res = df .loc [(["a " , "b " ], [2 , 1 ]), :]
2561
- exp_index = pd .MultiIndex .from_arrays ([["a " , "a " , "b " , "b " ], [2 , 1 , 2 , 1 ]])
2562
- tm .assert_index_equal (res .index , exp_index )
2563
+ res = df .loc [(["b " , "a " ], [2 , 1 ]), :]
2564
+ exp_index = pd .MultiIndex .from_arrays ([["b " , "b " , "a " , "a " ], [2 , 1 , 2 , 1 ]])
2565
+ tm .assert_index_equal (res .index , exp_index )
2563
2566
2564
- res = df .loc [(["b" , "a" ], [2 , 1 ]), :]
2565
- exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [2 , 1 , 2 , 1 ]])
2566
- tm .assert_index_equal (res .index , exp_index )
2567
+ res = df .loc [(["b" , "a" ], [1 , 2 ]), :]
2568
+ exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]])
2569
+ tm .assert_index_equal (res .index , exp_index )
2567
2570
2568
- res = df .loc [(["b" , "a" ], [1 , 2 ]), :]
2569
- exp_index = pd .MultiIndex .from_arrays ([["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]])
2570
- tm .assert_index_equal (res .index , exp_index )
2571
+ res = df .loc [:, ["Colorado" , "Ohio" ]]
2572
+ exp_columns = pd .MultiIndex .from_arrays (
2573
+ [["Colorado" , "Ohio" , "Ohio" ], ["Green" , "Green" , "Red" ]]
2574
+ )
2575
+ tm .assert_index_equal (res .columns , exp_columns )
2576
+
2577
+ res = df .loc [:, (["Colorado" , "Ohio" ], ["Red" , "Green" ])]
2578
+ exp_columns = pd .MultiIndex .from_arrays (
2579
+ [["Colorado" , "Ohio" , "Ohio" ], ["Green" , "Red" , "Green" ]]
2580
+ )
2581
+ tm .assert_index_equal (res .columns , exp_columns )
0 commit comments