Skip to content

Commit 24721da

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 39602e7 commit 24721da

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

0 commit comments

Comments
 (0)