@@ -2535,47 +2535,28 @@ 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 ):
2538
+ @pytest .mark .parametrize (
2539
+ "keys, expected" ,
2540
+ [
2541
+ (["b" , "a" ], [["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]]),
2542
+ (["a" , "b" ], [["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]]),
2543
+ ((["a" , "b" ], [1 , 2 ]), [["a" , "a" , "b" , "b" ], [1 , 2 , 1 , 2 ]]),
2544
+ ((["a" , "b" ], [2 , 1 ]), [["a" , "a" , "b" , "b" ], [2 , 1 , 2 , 1 ]]),
2545
+ ((["b" , "a" ], [2 , 1 ]), [["b" , "b" , "a" , "a" ], [2 , 1 , 2 , 1 ]]),
2546
+ ((["b" , "a" ], [1 , 2 ]), [["b" , "b" , "a" , "a" ], [1 , 2 , 1 , 2 ]]),
2547
+ ((["c" , "a" ], [2 , 1 ]), [["c" , "a" , "a" ], [1 , 2 , 1 ]]),
2548
+ ],
2549
+ )
2550
+ @pytest .mark .parametrize ("dim" , ["index" , "columns" ])
2551
+ def test_multilevel_index_loc_order (self , dim , keys , expected ):
2539
2552
# GH 22797
2540
2553
# 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
- )
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 )
2550
-
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 )
2554
-
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 )
2558
-
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 )
2562
-
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 )
2566
-
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 )
2570
-
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 )
2554
+ kwargs = {dim : [["c" , "a" , "a" , "b" , "b" ], [1 , 1 , 2 , 1 , 2 ]]}
2555
+ df = pd .DataFrame (np .arange (25 ).reshape (5 , 5 ), ** kwargs ,)
2556
+ exp_index = MultiIndex .from_arrays (expected )
2557
+ if dim == "index" :
2558
+ res = df .loc [keys , :]
2559
+ tm .assert_index_equal (res .index , exp_index )
2560
+ elif dim == "columns" :
2561
+ res = df .loc [:, keys ]
2562
+ tm .assert_index_equal (res .columns , exp_index )
0 commit comments