Skip to content

CLN: indexing comments and cleanups #32082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 23, 2020
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
8a9994a
getitem_lowerdim cleanups
jbrockmendel Feb 12, 2020
1fc0f93
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 12, 2020
0336d11
simplify get_label
jbrockmendel Feb 12, 2020
c006fa5
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 12, 2020
c415318
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 13, 2020
df731f1
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 14, 2020
61da1d8
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 14, 2020
8ed4cbe
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 14, 2020
dea9da5
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 17, 2020
9c75da8
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 18, 2020
2eb50d1
CLN: indexing
jbrockmendel Feb 18, 2020
e5fb3cf
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 18, 2020
2e3a3cd
CLN: indexing comments
jbrockmendel Feb 18, 2020
4081bbd
FIXUP: stray apostrophe
jbrockmendel Feb 18, 2020
2beeb1a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 19, 2020
894087e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 21, 2020
99c1c6c
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 22, 2020
eabf211
remove unnecessary check
jbrockmendel Feb 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,14 +732,15 @@ def _getitem_lowerdim(self, tup: Tuple):
raise IndexingError("Too many indexers. handle elsewhere")

for i, key in enumerate(tup):
if is_label_like(key) or isinstance(key, tuple):
if is_label_like(key):
# We don't need to check for tuples here because those are
# caught by the _is_nested_tuple_indexer check above.
section = self._getitem_axis(key, axis=i)

# we have yielded a scalar ?
if not is_list_like_indexer(section):
return section

elif section.ndim == self.ndim:
# We should never have a scalar section here, because
# _getitem_lowerdim is only called after a check for
# is_scalar_access, which that would be.
if section.ndim == self.ndim:
# we're in the middle of slicing through a MultiIndex
# revise the key wrt to `section` by inserting an _NS
new_key = tup[:i] + (_NS,) + tup[i + 1 :]
Expand All @@ -757,7 +758,7 @@ def _getitem_lowerdim(self, tup: Tuple):
# slice returns a new object.
if com.is_null_slice(new_key):
return section
# This is an elided recursive call to iloc/loc/etc'
# This is an elided recursive call to iloc/loc
return getattr(section, self.name)[new_key]

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

def _get_label(self, label, axis: int):
if self.ndim == 1:
# for perf reasons we want to try _xs first
# as its basically direct indexing
# but will fail when the index is not present
# see GH5667
return self.obj._xs(label, axis=axis)
elif isinstance(label, tuple) and isinstance(label[axis], slice):
raise IndexingError("no slices here, handle elsewhere")

# GH#5667 this will fail if the label is not present in the axis.
return self.obj._xs(label, axis=axis)

def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):
Expand Down Expand Up @@ -1298,7 +1291,7 @@ def _validate_read_indexer(

# We (temporarily) allow for some missing keys with .loc, except in
# some cases (e.g. setting) in which "raise_missing" will be False
if not (self.name == "loc" and not raise_missing):
if raise_missing:
not_found = list(set(key) - set(ax))
raise KeyError(f"{not_found} not in index")

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

def _has_valid_setitem_indexer(self, indexer):
self._has_valid_positional_setitem_indexer(indexer)

def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
def _has_valid_setitem_indexer(self, indexer) -> bool:
"""
Validate that a positional indexer cannot enlarge its target
will raise if needed, does not modify the indexer externally.
Expand All @@ -1376,7 +1366,7 @@ def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
bool
"""
if isinstance(indexer, dict):
raise IndexError(f"{self.name} cannot enlarge its target object")
raise IndexError("iloc cannot enlarge its target object")
else:
if not isinstance(indexer, tuple):
indexer = _tuplify(self.ndim, indexer)
Expand All @@ -1389,11 +1379,9 @@ def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
pass
elif is_integer(i):
if i >= len(ax):
raise IndexError(
f"{self.name} cannot enlarge its target object"
)
raise IndexError("iloc cannot enlarge its target object")
elif isinstance(i, dict):
raise IndexError(f"{self.name} cannot enlarge its target object")
raise IndexError("iloc cannot enlarge its target object")

return True

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

elif is_float(key):
# _validate_indexer call will always raise
labels._validate_indexer("positional", key, "iloc")
return key

self._validate_key(key, axis)
return key
Expand Down Expand Up @@ -1582,7 +1570,7 @@ def _setitem_with_indexer(self, indexer, value):
# this correctly sets the dtype and avoids cache issues
# essentially this separates out the block that is needed
# to possibly be modified
if self.ndim > 1 and i == self.obj._info_axis_number:
if self.ndim > 1 and i == info_axis:

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