@@ -732,14 +732,15 @@ def _getitem_lowerdim(self, tup: Tuple):
732
732
raise IndexingError ("Too many indexers. handle elsewhere" )
733
733
734
734
for i , key in enumerate (tup ):
735
- if is_label_like (key ) or isinstance (key , tuple ):
735
+ if is_label_like (key ):
736
+ # We don't need to check for tuples here because those are
737
+ # caught by the _is_nested_tuple_indexer check above.
736
738
section = self ._getitem_axis (key , axis = i )
737
739
738
- # we have yielded a scalar ?
739
- if not is_list_like_indexer (section ):
740
- return section
741
-
742
- elif section .ndim == self .ndim :
740
+ # We should never have a scalar section here, because
741
+ # _getitem_lowerdim is only called after a check for
742
+ # is_scalar_access, which that would be.
743
+ if section .ndim == self .ndim :
743
744
# we're in the middle of slicing through a MultiIndex
744
745
# revise the key wrt to `section` by inserting an _NS
745
746
new_key = tup [:i ] + (_NS ,) + tup [i + 1 :]
@@ -757,7 +758,7 @@ def _getitem_lowerdim(self, tup: Tuple):
757
758
# slice returns a new object.
758
759
if com .is_null_slice (new_key ):
759
760
return section
760
- # This is an elided recursive call to iloc/loc/etc'
761
+ # This is an elided recursive call to iloc/loc
761
762
return getattr (section , self .name )[new_key ]
762
763
763
764
raise IndexingError ("not applicable" )
@@ -1013,15 +1014,7 @@ def _getitem_tuple(self, tup: Tuple):
1013
1014
return self ._getitem_tuple_same_dim (tup )
1014
1015
1015
1016
def _get_label (self , label , axis : int ):
1016
- if self .ndim == 1 :
1017
- # for perf reasons we want to try _xs first
1018
- # as its basically direct indexing
1019
- # but will fail when the index is not present
1020
- # see GH5667
1021
- return self .obj ._xs (label , axis = axis )
1022
- elif isinstance (label , tuple ) and isinstance (label [axis ], slice ):
1023
- raise IndexingError ("no slices here, handle elsewhere" )
1024
-
1017
+ # GH#5667 this will fail if the label is not present in the axis.
1025
1018
return self .obj ._xs (label , axis = axis )
1026
1019
1027
1020
def _handle_lowerdim_multi_index_axis0 (self , tup : Tuple ):
@@ -1298,7 +1291,7 @@ def _validate_read_indexer(
1298
1291
1299
1292
# We (temporarily) allow for some missing keys with .loc, except in
1300
1293
# some cases (e.g. setting) in which "raise_missing" will be False
1301
- if not ( self . name == "loc" and not raise_missing ) :
1294
+ if raise_missing :
1302
1295
not_found = list (set (key ) - set (ax ))
1303
1296
raise KeyError (f"{ not_found } not in index" )
1304
1297
@@ -1363,10 +1356,7 @@ def _validate_key(self, key, axis: int):
1363
1356
else :
1364
1357
raise ValueError (f"Can only index by location with a [{ self ._valid_types } ]" )
1365
1358
1366
- def _has_valid_setitem_indexer (self , indexer ):
1367
- self ._has_valid_positional_setitem_indexer (indexer )
1368
-
1369
- def _has_valid_positional_setitem_indexer (self , indexer ) -> bool :
1359
+ def _has_valid_setitem_indexer (self , indexer ) -> bool :
1370
1360
"""
1371
1361
Validate that a positional indexer cannot enlarge its target
1372
1362
will raise if needed, does not modify the indexer externally.
@@ -1376,7 +1366,7 @@ def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
1376
1366
bool
1377
1367
"""
1378
1368
if isinstance (indexer , dict ):
1379
- raise IndexError (f" { self . name } cannot enlarge its target object" )
1369
+ raise IndexError ("iloc cannot enlarge its target object" )
1380
1370
else :
1381
1371
if not isinstance (indexer , tuple ):
1382
1372
indexer = _tuplify (self .ndim , indexer )
@@ -1389,11 +1379,9 @@ def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
1389
1379
pass
1390
1380
elif is_integer (i ):
1391
1381
if i >= len (ax ):
1392
- raise IndexError (
1393
- f"{ self .name } cannot enlarge its target object"
1394
- )
1382
+ raise IndexError ("iloc cannot enlarge its target object" )
1395
1383
elif isinstance (i , dict ):
1396
- raise IndexError (f" { self . name } cannot enlarge its target object" )
1384
+ raise IndexError ("iloc cannot enlarge its target object" )
1397
1385
1398
1386
return True
1399
1387
@@ -1520,8 +1508,8 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
1520
1508
return key
1521
1509
1522
1510
elif is_float (key ):
1511
+ # _validate_indexer call will always raise
1523
1512
labels ._validate_indexer ("positional" , key , "iloc" )
1524
- return key
1525
1513
1526
1514
self ._validate_key (key , axis )
1527
1515
return key
@@ -1582,7 +1570,7 @@ def _setitem_with_indexer(self, indexer, value):
1582
1570
# this correctly sets the dtype and avoids cache issues
1583
1571
# essentially this separates out the block that is needed
1584
1572
# to possibly be modified
1585
- if self .ndim > 1 and i == self . obj . _info_axis_number :
1573
+ if self .ndim > 1 and i == info_axis :
1586
1574
1587
1575
# add the new item, and set the value
1588
1576
# must have all defined axes if we have a scalar
0 commit comments