Skip to content

CLN: inline indexing 1-liners #31625

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 11 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3498,7 +3498,9 @@ def _iget_item_cache(self, item):
def _box_item_values(self, key, values):
raise AbstractMethodError(self)

def _slice(self: FrameOrSeries, slobj: slice, axis=0, kind=None) -> FrameOrSeries:
def _slice(
self: FrameOrSeries, slobj: slice, axis=0, kind: str = "getitem"
) -> FrameOrSeries:
"""
Construct a slice of this container.

Expand Down
39 changes: 13 additions & 26 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,6 @@ def _get_label(self, label, axis: int):

return self.obj._xs(label, axis=axis)

def _get_loc(self, key: int, axis: int):
return self.obj._ixs(key, axis=axis)

def _slice(self, obj, axis: int, kind=None):
return self.obj._slice(obj, axis=axis, kind=kind)

def _get_setitem_indexer(self, key):
if self.axis is not None:
return self._convert_tuple(key, is_setter=True)
Expand Down Expand Up @@ -702,17 +696,6 @@ def _convert_tuple(self, key, is_setter: bool = False):
keyidx.append(idx)
return tuple(keyidx)

def _convert_scalar_indexer(self, key, axis: int):
# if we are accessing via lowered dim, use the last dim
ax = self.obj._get_axis(min(axis, self.ndim - 1))
# a scalar
return ax._convert_scalar_indexer(key, kind=self.name)

def _convert_slice_indexer(self, key: slice, axis: int):
# if we are accessing via lowered dim, use the last dim
ax = self.obj._get_axis(min(axis, self.ndim - 1))
return ax._convert_slice_indexer(key, kind=self.name)

def _has_valid_setitem_indexer(self, indexer) -> bool:
return True

Expand Down Expand Up @@ -1627,7 +1610,8 @@ def _validate_key(self, key, axis: int):
return

if not is_list_like_indexer(key):
self._convert_scalar_indexer(key, axis)
labels = self.obj._get_axis(axis)
labels._convert_scalar_indexer(key, kind="loc")

def _is_scalar_access(self, key: Tuple) -> bool:
"""
Expand Down Expand Up @@ -1772,7 +1756,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int):
)

if isinstance(indexer, slice):
return self._slice(indexer, axis=axis, kind="iloc")
return self.obj._slice(indexer, axis=axis, kind="iloc")
else:
# DatetimeIndex overrides Index.slice_indexer and may
# return a DatetimeIndex instead of a slice object.
Expand All @@ -1796,12 +1780,12 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
labels = self.obj._get_axis(axis)

if isinstance(key, slice):
return self._convert_slice_indexer(key, axis)
return labels._convert_slice_indexer(key, kind="loc")

if is_scalar(key):
# try to find out correct indexer, if not type correct raise
try:
key = self._convert_scalar_indexer(key, axis)
key = labels._convert_scalar_indexer(key, kind="loc")
except TypeError:
# but we will allow setting
if not is_setter:
Expand Down Expand Up @@ -2025,7 +2009,7 @@ def _getitem_axis(self, key, axis: int):
# validate the location
self._validate_integer(key, axis)

return self._get_loc(key, axis=axis)
return self.obj._ixs(key, axis=axis)

def _get_slice_axis(self, slice_obj: slice, axis: int):
# caller is responsible for ensuring non-None axis
Expand All @@ -2034,19 +2018,22 @@ def _get_slice_axis(self, slice_obj: slice, axis: int):
if not need_slice(slice_obj):
return obj.copy(deep=False)

indexer = self._convert_slice_indexer(slice_obj, axis)
return self._slice(indexer, axis=axis, kind="iloc")
labels = obj._get_axis(axis)
indexer = labels._convert_slice_indexer(slice_obj, kind="iloc")
return self.obj._slice(indexer, axis=axis, kind="iloc")

def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
"""
Much simpler as we only have to deal with our valid types.
"""
labels = self.obj._get_axis(axis)

# make need to convert a float key
if isinstance(key, slice):
return self._convert_slice_indexer(key, axis)
return labels._convert_slice_indexer(key, kind="iloc")

elif is_float(key):
return self._convert_scalar_indexer(key, axis)
return labels._convert_scalar_indexer(key, kind="iloc")

self._validate_key(key, axis)
return key
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,9 @@ def _ixs(self, i: int, axis: int = 0):
"""
return self._values[i]

def _slice(self, slobj: slice, axis: int = 0, kind=None) -> "Series":
slobj = self.index._convert_slice_indexer(slobj, kind=kind or "getitem")
def _slice(self, slobj: slice, axis: int = 0, kind: str = "getitem") -> "Series":
assert kind in ["getitem", "iloc"]
slobj = self.index._convert_slice_indexer(slobj, kind=kind)
return self._get_values(slobj)

def __getitem__(self, key):
Expand Down