Skip to content

Commit d6080c0

Browse files
committed
TST: Test also for columns
1 parent e81bd52 commit d6080c0

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
@@ -2440,36 +2440,47 @@ def test_sort_ascending_list(self):
24402440
expected = s.iloc[[0, 4, 1, 5, 2, 6, 3, 7]]
24412441
tm.assert_series_equal(result, expected)
24422442

2443+
def test_multiindex_loc_order(self):
2444+
# GH 22797
2445+
# Try to respect order of keys given for MultiIndex.loc
2446+
df = pd.DataFrame(
2447+
np.arange(12).reshape((4, 3)),
2448+
index=[["a", "a", "b", "b"], [1, 2, 1, 2]],
2449+
columns=[["Ohio", "Ohio", "Colorado"], ["Green", "Red", "Green"]],
2450+
)
24432451

2444-
def test_multiindex_loc_order():
2445-
# GH 22797
2446-
# Try to respect order of keys given for MultiIndex.loc
2447-
df = pd.DataFrame(
2448-
np.arange(12).reshape((4, 3)),
2449-
index=[["a", "a", "b", "b"], [1, 2, 1, 2]],
2450-
columns=[["Ohio", "Ohio", "Colorado"], ["Green", "Red", "Green"]],
2451-
)
2452+
res = df.loc[["b", "a"], :]
2453+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
2454+
tm.assert_index_equal(res.index, exp_index)
24522455

2453-
res = df.loc[["b", "a"], :]
2454-
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
2455-
tm.assert_index_equal(res.index, exp_index)
2456+
res = df.loc[["a", "b"], :]
2457+
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [1, 2, 1, 2]])
2458+
tm.assert_index_equal(res.index, exp_index)
24562459

2457-
res = df.loc[["a", "b"], :]
2458-
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [1, 2, 1, 2]])
2459-
tm.assert_index_equal(res.index, exp_index)
2460+
res = df.loc[(["a", "b"], [1, 2]), :]
2461+
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [1, 2, 1, 2]])
2462+
tm.assert_index_equal(res.index, exp_index)
24602463

2461-
res = df.loc[(["a", "b"], [1, 2]), :]
2462-
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [1, 2, 1, 2]])
2463-
tm.assert_index_equal(res.index, exp_index)
2464+
res = df.loc[(["a", "b"], [2, 1]), :]
2465+
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [2, 1, 2, 1]])
2466+
tm.assert_index_equal(res.index, exp_index)
24642467

2465-
res = df.loc[(["a", "b"], [2, 1]), :]
2466-
exp_index = pd.MultiIndex.from_arrays([["a", "a", "b", "b"], [2, 1, 2, 1]])
2467-
tm.assert_index_equal(res.index, exp_index)
2468+
res = df.loc[(["b", "a"], [2, 1]), :]
2469+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [2, 1, 2, 1]])
2470+
tm.assert_index_equal(res.index, exp_index)
24682471

2469-
res = df.loc[(["b", "a"], [2, 1]), :]
2470-
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [2, 1, 2, 1]])
2471-
tm.assert_index_equal(res.index, exp_index)
2472+
res = df.loc[(["b", "a"], [1, 2]), :]
2473+
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
2474+
tm.assert_index_equal(res.index, exp_index)
24722475

2473-
res = df.loc[(["b", "a"], [1, 2]), :]
2474-
exp_index = pd.MultiIndex.from_arrays([["b", "b", "a", "a"], [1, 2, 1, 2]])
2475-
tm.assert_index_equal(res.index, exp_index)
2476+
res = df.loc[:, ["Colorado", "Ohio"]]
2477+
exp_columns = pd.MultiIndex.from_arrays(
2478+
[["Colorado", "Ohio", "Ohio"], ["Green", "Green", "Red"]]
2479+
)
2480+
tm.assert_index_equal(res.columns, exp_columns)
2481+
2482+
res = df.loc[:, (["Colorado", "Ohio"], ["Red", "Green"])]
2483+
exp_columns = pd.MultiIndex.from_arrays(
2484+
[["Colorado", "Ohio", "Ohio"], ["Green", "Red", "Green"]]
2485+
)
2486+
tm.assert_index_equal(res.columns, exp_columns)

0 commit comments

Comments
 (0)