Skip to content

Commit a31d603

Browse files
committed
TST: Test for issue #22797
Testing return order of MultiIndex.loc MultiIndex.loc try to return the result in the same order as the key given.
1 parent 966757f commit a31d603

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
@@ -437,3 +437,21 @@ def test_timestamp_multiindex_indexer():
437437
)
438438
should_be = pd.Series(data=np.arange(24, len(qidx) + 24), index=qidx, name="foo")
439439
tm.assert_series_equal(result, should_be)
440+
441+
442+
def test_multiindex_loc_order():
443+
# GH 22797
444+
# Try to respect order of keys given for MultiIndex.loc
445+
df = pd.DataFrame(
446+
np.arange(12).reshape((4, 3)),
447+
index=[["a", "a", "b", "b"], [1, 2, 1, 2]],
448+
columns=[["Ohio", "Ohio", "Colorado"], ["Green", "Red", "Green"]],
449+
)
450+
451+
res = df.loc[["b", "a"], :]
452+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
453+
tm.assert_index_equal(res.index, exp_index)
454+
455+
res = df.loc[(["b", "a"], [2, 1]), :]
456+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [2, 1, 2, 1]])
457+
tm.assert_index_equal(res.index, exp_index)

0 commit comments

Comments
 (0)