Skip to content

Commit 4bd7e62

Browse files
committed
TST: Test for issue pandas-dev#22797
Testing return order of MultiIndex.loc MultiIndex.loc try to return the result in the same order as the key given.
1 parent 6e9651e commit 4bd7e62

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/tests/indexes/multi/test_indexing.py

+18
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,21 @@ def test_slice_locs_with_missing_value(index_arr, expected, start_idx, end_idx):
525525
idx = MultiIndex.from_arrays(index_arr)
526526
result = idx.slice_locs(start=start_idx, end=end_idx)
527527
assert result == expected
528+
529+
530+
def test_multiindex_loc_order():
531+
# GH 22797
532+
# Try to respect order of keys given for MultiIndex.loc
533+
df = pd.DataFrame(
534+
np.arange(12).reshape((4, 3)),
535+
index=[["a", "a", "b", "b"], [1, 2, 1, 2]],
536+
columns=[["Ohio", "Ohio", "Colorado"], ["Green", "Red", "Green"]],
537+
)
538+
539+
res = df.loc[["b", "a"], :]
540+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
541+
tm.assert_index_equal(res.index, exp_index)
542+
543+
res = df.loc[(["b", "a"], [2, 1]), :]
544+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [2, 1, 2, 1]])
545+
tm.assert_index_equal(res.index, exp_index)

0 commit comments

Comments
 (0)