Skip to content

Commit 58df0ac

Browse files
authored
CLN: indexing comments and cleanups (#32082)
1 parent 034fab5 commit 58df0ac

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

Diff for: pandas/core/indexing.py

+16-28
Original file line numberDiff line numberDiff line change
@@ -732,14 +732,15 @@ def _getitem_lowerdim(self, tup: Tuple):
732732
raise IndexingError("Too many indexers. handle elsewhere")
733733

734734
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.
736738
section = self._getitem_axis(key, axis=i)
737739

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:
743744
# we're in the middle of slicing through a MultiIndex
744745
# revise the key wrt to `section` by inserting an _NS
745746
new_key = tup[:i] + (_NS,) + tup[i + 1 :]
@@ -757,7 +758,7 @@ def _getitem_lowerdim(self, tup: Tuple):
757758
# slice returns a new object.
758759
if com.is_null_slice(new_key):
759760
return section
760-
# This is an elided recursive call to iloc/loc/etc'
761+
# This is an elided recursive call to iloc/loc
761762
return getattr(section, self.name)[new_key]
762763

763764
raise IndexingError("not applicable")
@@ -1013,15 +1014,7 @@ def _getitem_tuple(self, tup: Tuple):
10131014
return self._getitem_tuple_same_dim(tup)
10141015

10151016
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.
10251018
return self.obj._xs(label, axis=axis)
10261019

10271020
def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):
@@ -1298,7 +1291,7 @@ def _validate_read_indexer(
12981291

12991292
# We (temporarily) allow for some missing keys with .loc, except in
13001293
# 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:
13021295
not_found = list(set(key) - set(ax))
13031296
raise KeyError(f"{not_found} not in index")
13041297

@@ -1363,10 +1356,7 @@ def _validate_key(self, key, axis: int):
13631356
else:
13641357
raise ValueError(f"Can only index by location with a [{self._valid_types}]")
13651358

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:
13701360
"""
13711361
Validate that a positional indexer cannot enlarge its target
13721362
will raise if needed, does not modify the indexer externally.
@@ -1376,7 +1366,7 @@ def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
13761366
bool
13771367
"""
13781368
if isinstance(indexer, dict):
1379-
raise IndexError(f"{self.name} cannot enlarge its target object")
1369+
raise IndexError("iloc cannot enlarge its target object")
13801370
else:
13811371
if not isinstance(indexer, tuple):
13821372
indexer = _tuplify(self.ndim, indexer)
@@ -1389,11 +1379,9 @@ def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
13891379
pass
13901380
elif is_integer(i):
13911381
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")
13951383
elif isinstance(i, dict):
1396-
raise IndexError(f"{self.name} cannot enlarge its target object")
1384+
raise IndexError("iloc cannot enlarge its target object")
13971385

13981386
return True
13991387

@@ -1520,8 +1508,8 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
15201508
return key
15211509

15221510
elif is_float(key):
1511+
# _validate_indexer call will always raise
15231512
labels._validate_indexer("positional", key, "iloc")
1524-
return key
15251513

15261514
self._validate_key(key, axis)
15271515
return key
@@ -1582,7 +1570,7 @@ def _setitem_with_indexer(self, indexer, value):
15821570
# this correctly sets the dtype and avoids cache issues
15831571
# essentially this separates out the block that is needed
15841572
# 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:
15861574

15871575
# add the new item, and set the value
15881576
# must have all defined axes if we have a scalar

0 commit comments

Comments
 (0)