Skip to content

Commit d8ec6c8

Browse files
committed
TST: Test also for columns
1 parent 9340916 commit d8ec6c8

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

pandas/tests/test_multilevel.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,36 +2535,47 @@ def test_sort_ascending_list(self):
25352535
expected = s.iloc[[0, 4, 1, 5, 2, 6, 3, 7]]
25362536
tm.assert_series_equal(result, expected)
25372537

2538+
def test_multiindex_loc_order(self):
2539+
# GH 22797
2540+
# 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+
)
25382546

2539-
def test_multiindex_loc_order():
2540-
# GH 22797
2541-
# Try to respect order of keys given for MultiIndex.loc
2542-
df = pd.DataFrame(
2543-
np.arange(12).reshape((4, 3)),
2544-
index=[["a", "a", "b", "b"], [1, 2, 1, 2]],
2545-
columns=[["Ohio", "Ohio", "Colorado"], ["Green", "Red", "Green"]],
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)
25472550

2548-
res = df.loc[["b", "a"], :]
2549-
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
2550-
tm.assert_index_equal(res.index, exp_index)
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)
25512554

2552-
res = df.loc[["a", "b"], :]
2553-
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [1, 2, 1, 2]])
2554-
tm.assert_index_equal(res.index, exp_index)
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)
25552558

2556-
res = df.loc[(["a", "b"], [1, 2]), :]
2557-
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [1, 2, 1, 2]])
2558-
tm.assert_index_equal(res.index, exp_index)
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)
25592562

2560-
res = df.loc[(["a", "b"], [2, 1]), :]
2561-
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [2, 1, 2, 1]])
2562-
tm.assert_index_equal(res.index, exp_index)
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)
25632566

2564-
res = df.loc[(["b", "a"], [2, 1]), :]
2565-
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [2, 1, 2, 1]])
2566-
tm.assert_index_equal(res.index, exp_index)
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)
25672570

2568-
res = df.loc[(["b", "a"], [1, 2]), :]
2569-
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
2570-
tm.assert_index_equal(res.index, exp_index)
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)

0 commit comments

Comments
 (0)