Skip to content

Commit bf8d495

Browse files
pilkibunjreback
pilkibun
authored andcommitted
TST: add regression test for slicing IntervalIndex MI level with scalars (#27572)
1 parent 3fb865e commit bf8d495

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pandas/tests/indexing/interval/test_interval.py

+35
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,38 @@ def test_loc_getitem_frame(self):
112112
# partial missing
113113
with pytest.raises(KeyError, match="^$"):
114114
df.loc[[10, 4]]
115+
116+
117+
class TestIntervalIndexInsideMultiIndex:
118+
def test_mi_intervalindex_slicing_with_scalar(self):
119+
# GH#27456
120+
idx = pd.MultiIndex.from_arrays(
121+
[
122+
pd.Index(["FC", "FC", "FC", "FC", "OWNER", "OWNER", "OWNER", "OWNER"]),
123+
pd.Index(
124+
["RID1", "RID1", "RID2", "RID2", "RID1", "RID1", "RID2", "RID2"]
125+
),
126+
pd.IntervalIndex.from_arrays(
127+
[0, 1, 10, 11, 0, 1, 10, 11], [1, 2, 11, 12, 1, 2, 11, 12]
128+
),
129+
]
130+
)
131+
132+
idx.names = ["Item", "RID", "MP"]
133+
df = pd.DataFrame({"value": [1, 2, 3, 4, 5, 6, 7, 8]})
134+
df.index = idx
135+
query_df = pd.DataFrame(
136+
{
137+
"Item": ["FC", "OWNER", "FC", "OWNER", "OWNER"],
138+
"RID": ["RID1", "RID1", "RID1", "RID2", "RID2"],
139+
"MP": [0.2, 1.5, 1.6, 11.1, 10.9],
140+
}
141+
)
142+
143+
query_df = query_df.sort_index()
144+
145+
idx = pd.MultiIndex.from_arrays([query_df.Item, query_df.RID, query_df.MP])
146+
query_df.index = idx
147+
result = df.value.loc[query_df.index]
148+
expected = pd.Series([1, 6, 2, 8, 7], index=idx, name="value")
149+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)