Skip to content

CLN: _convert_scalar_indexer only handle "loc" and "getitem" #31709

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 10 commits into from
Feb 7, 2020
Merged
10 changes: 3 additions & 7 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3100,20 +3100,16 @@ def _filter_indexer_tolerance(
# --------------------------------------------------------------------
# Indexer Conversion Methods

def _convert_scalar_indexer(self, key, kind=None):
def _convert_scalar_indexer(self, key, kind: str_t):
"""
Convert a scalar indexer.

Parameters
----------
key : label of the slice bound
kind : {'loc', 'getitem', 'iloc'} or None
kind : {'loc', 'getitem'}
"""
assert kind in ["loc", "getitem", "iloc", None]

if kind == "iloc":
self._validate_indexer("positional", key, "iloc")
return key
assert kind in ["loc", "getitem"]

if len(self) and not isinstance(self, ABCMultiIndex):

Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,11 @@ def get_indexer_non_unique(self, target):
return ensure_platform_int(indexer), missing

@Appender(Index._convert_scalar_indexer.__doc__)
def _convert_scalar_indexer(self, key, kind=None):
def _convert_scalar_indexer(self, key, kind: str):
assert kind in ["loc", "getitem"]
if kind == "loc":
try:
return self.categories._convert_scalar_indexer(key, kind=kind)
return self.categories._convert_scalar_indexer(key, kind="loc")
except TypeError:
self._invalid_indexer("label", key)
return super()._convert_scalar_indexer(key, kind=kind)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,18 @@ def _format_attrs(self):
# --------------------------------------------------------------------
# Indexing Methods

def _convert_scalar_indexer(self, key, kind=None):
def _convert_scalar_indexer(self, key, kind: str):
"""
We don't allow integer or float indexing on datetime-like when using
loc.

Parameters
----------
key : label of the slice bound
kind : {'loc', 'getitem', 'iloc'} or None
kind : {'loc', 'getitem'}
"""

assert kind in ["loc", "getitem", "iloc", None]
assert kind in ["loc", "getitem"]

if not is_scalar(key):
raise TypeError(key)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ def _should_fallback_to_positional(self):
return self.dtype.subtype.kind in ["m", "M"]

@Appender(Index._convert_scalar_indexer.__doc__)
def _convert_scalar_indexer(self, key, kind=None):
if kind == "iloc":
return super()._convert_scalar_indexer(key, kind=kind)
def _convert_scalar_indexer(self, key, kind: str):
assert kind in ["getitem", "loc"]
# never iloc, so no-op
return key

def _maybe_cast_slice_bound(self, label, side, kind):
Expand Down
18 changes: 7 additions & 11 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,11 @@ def asi8(self) -> np.ndarray:
return self.values.view(self._default_dtype)

@Appender(Index._convert_scalar_indexer.__doc__)
def _convert_scalar_indexer(self, key, kind=None):
assert kind in ["loc", "getitem", "iloc", None]
def _convert_scalar_indexer(self, key, kind: str):
assert kind in ["loc", "getitem"]

# don't coerce ilocs to integers
if kind != "iloc":
key = self._maybe_cast_indexer(key)
# never iloc, which we don't coerce to integers
key = self._maybe_cast_indexer(key)
return super()._convert_scalar_indexer(key, kind=kind)


Expand Down Expand Up @@ -388,12 +387,9 @@ def _should_fallback_to_positional(self):
return False

@Appender(Index._convert_scalar_indexer.__doc__)
def _convert_scalar_indexer(self, key, kind=None):
assert kind in ["loc", "getitem", "iloc", None]

if kind == "iloc":
self._validate_indexer("positional", key, "iloc")

def _convert_scalar_indexer(self, key, kind: str):
assert kind in ["loc", "getitem"]
# no-op for non-iloc
return key

@Appender(Index._convert_slice_indexer.__doc__)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2033,7 +2033,8 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
return labels._convert_slice_indexer(key, kind="iloc")

elif is_float(key):
return labels._convert_scalar_indexer(key, kind="iloc")
labels._validate_indexer("positional", key, "iloc")
return key

self._validate_key(key, axis)
return key
Expand Down