@@ -130,6 +130,19 @@ def test_loc_multiindex_missing_label_raises(self):
130
130
with pytest .raises (KeyError , match = r"^2$" ):
131
131
df .loc [2 ]
132
132
133
+ @pytest .mark .parametrize ("key, pos" , [([2 , 4 ], [0 , 1 ]), ([2 ], []), ([2 , 3 ], [])])
134
+ def test_loc_multiindex_list_missing_label (self , key , pos ):
135
+ # GH 27148 - lists with missing labels do not raise:
136
+ df = DataFrame (
137
+ np .random .randn (3 , 3 ),
138
+ columns = [[2 , 2 , 4 ], [6 , 8 , 10 ]],
139
+ index = [[4 , 4 , 8 ], [8 , 10 , 12 ]],
140
+ )
141
+
142
+ expected = df .iloc [pos ]
143
+ result = df .loc [key ]
144
+ tm .assert_frame_equal (result , expected )
145
+
133
146
def test_loc_multiindex_too_many_dims_raises (self ):
134
147
# GH 14885
135
148
s = Series (
@@ -280,47 +293,27 @@ def convert_nested_indexer(indexer_type, keys):
280
293
281
294
282
295
@pytest .mark .parametrize (
283
- "indexer, is_level1, expected_error " ,
296
+ "indexer, pos " ,
284
297
[
285
- ([], False , None ), # empty ok
286
- (["A" ], False , None ),
287
- (["A" , "D" ], False , None ),
288
- (["D" ], False , r"\['D'\] not in index" ), # not any values found
289
- (pd .IndexSlice [:, ["foo" ]], True , None ),
290
- (pd .IndexSlice [:, ["foo" , "bah" ]], True , None ),
298
+ ([], []), # empty ok
299
+ (["A" ], slice (3 )),
300
+ (["A" , "D" ], slice (3 )),
301
+ (["D" , "E" ], []), # no values found - fine
302
+ (["D" ], []), # same, with single item list: GH 27148
303
+ (pd .IndexSlice [:, ["foo" ]], slice (2 , None , 3 )),
304
+ (pd .IndexSlice [:, ["foo" , "bah" ]], slice (2 , None , 3 )),
291
305
],
292
306
)
293
- def test_loc_getitem_duplicates_multiindex_missing_indexers (
294
- indexer , is_level1 , expected_error
295
- ):
307
+ def test_loc_getitem_duplicates_multiindex_missing_indexers (indexer , pos ):
296
308
# GH 7866
297
309
# multi-index slicing with missing indexers
298
310
idx = MultiIndex .from_product (
299
311
[["A" , "B" , "C" ], ["foo" , "bar" , "baz" ]], names = ["one" , "two" ]
300
312
)
301
313
s = Series (np .arange (9 , dtype = "int64" ), index = idx ).sort_index ()
302
-
303
- if indexer == []:
304
- expected = s .iloc [[]]
305
- elif is_level1 :
306
- expected = Series (
307
- [0 , 3 , 6 ],
308
- index = MultiIndex .from_product (
309
- [["A" , "B" , "C" ], ["foo" ]], names = ["one" , "two" ]
310
- ),
311
- ).sort_index ()
312
- else :
313
- exp_idx = MultiIndex .from_product (
314
- [["A" ], ["foo" , "bar" , "baz" ]], names = ["one" , "two" ]
315
- )
316
- expected = Series (np .arange (3 , dtype = "int64" ), index = exp_idx ).sort_index ()
317
-
318
- if expected_error is not None :
319
- with pytest .raises (KeyError , match = expected_error ):
320
- s .loc [indexer ]
321
- else :
322
- result = s .loc [indexer ]
323
- tm .assert_series_equal (result , expected )
314
+ expected = s .iloc [pos ]
315
+ result = s .loc [indexer ]
316
+ tm .assert_series_equal (result , expected )
324
317
325
318
326
319
def test_series_loc_getitem_fancy (multiindex_year_month_day_dataframe_random_data ):
0 commit comments