Skip to content

Commit cd939c6

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 ca3bfcc commit cd939c6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pandas/tests/indexes/multi/test_indexing.py

+19
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ 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+
<<<<<<< HEAD
441442

442443

443444
def test_get_loc_with_values_including_missing_values():
@@ -526,3 +527,21 @@ def test_slice_locs_with_missing_value(index_arr, expected, start_idx, end_idx):
526527
idx = MultiIndex.from_arrays(index_arr)
527528
result = idx.slice_locs(start=start_idx, end=end_idx)
528529
assert result == expected
530+
531+
532+
def test_multiindex_loc_order():
533+
# GH 22797
534+
# Try to respect order of keys given for MultiIndex.loc
535+
df = pd.DataFrame(
536+
np.arange(12).reshape((4, 3)),
537+
index=[["a", "a", "b", "b"], [1, 2, 1, 2]],
538+
columns=[["Ohio", "Ohio", "Colorado"], ["Green", "Red", "Green"]],
539+
)
540+
541+
res = df.loc[["b", "a"], :]
542+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
543+
tm.assert_index_equal(res.index, exp_index)
544+
545+
res = df.loc[(["b", "a"], [2, 1]), :]
546+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [2, 1, 2, 1]])
547+
tm.assert_index_equal(res.index, exp_index)

0 commit comments

Comments
 (0)