@@ -134,16 +134,15 @@ def test_loc_multiindex_missing_label_raises(self):
134
134
135
135
@pytest .mark .parametrize ("key, pos" , [([2 , 4 ], [0 , 1 ]), ([2 ], []), ([2 , 3 ], [])])
136
136
def test_loc_multiindex_list_missing_label (self , key , pos ):
137
- # GH 27148 - lists with missing labels do not raise:
137
+ # GH 27148 - lists with missing labels _do_ raise
138
138
df = DataFrame (
139
139
np .random .randn (3 , 3 ),
140
140
columns = [[2 , 2 , 4 ], [6 , 8 , 10 ]],
141
141
index = [[4 , 4 , 8 ], [8 , 10 , 12 ]],
142
142
)
143
143
144
- expected = df .iloc [pos ]
145
- result = df .loc [key ]
146
- tm .assert_frame_equal (result , expected )
144
+ with pytest .raises (KeyError , match = "not in index" ):
145
+ df .loc [key ]
147
146
148
147
def test_loc_multiindex_too_many_dims_raises (self ):
149
148
# GH 14885
@@ -295,8 +294,8 @@ def convert_nested_indexer(indexer_type, keys):
295
294
[
296
295
([], []), # empty ok
297
296
(["A" ], slice (3 )),
298
- (["A" , "D" ], slice ( 3 )),
299
- (["D" , "E" ], []), # no values found - fine
297
+ (["A" , "D" ], []), # "D" isnt present -> raise
298
+ (["D" , "E" ], []), # no values found -> raise
300
299
(["D" ], []), # same, with single item list: GH 27148
301
300
(pd .IndexSlice [:, ["foo" ]], slice (2 , None , 3 )),
302
301
(pd .IndexSlice [:, ["foo" , "bah" ]], slice (2 , None , 3 )),
@@ -310,8 +309,13 @@ def test_loc_getitem_duplicates_multiindex_missing_indexers(indexer, pos):
310
309
)
311
310
s = Series (np .arange (9 , dtype = "int64" ), index = idx ).sort_index ()
312
311
expected = s .iloc [pos ]
313
- result = s .loc [indexer ]
314
- tm .assert_series_equal (result , expected )
312
+
313
+ if expected .size == 0 and indexer != []:
314
+ with pytest .raises (KeyError , match = str (indexer )):
315
+ s .loc [indexer ]
316
+ else :
317
+ result = s .loc [indexer ]
318
+ tm .assert_series_equal (result , expected )
315
319
316
320
317
321
def test_series_loc_getitem_fancy (multiindex_year_month_day_dataframe_random_data ):
0 commit comments