Skip to content

REF: remove _convert_scalar_indexer #31962

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 17 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
d974f28
API: always raise KeyError on __getitem__ and loc.__getitem__, never …
jbrockmendel Feb 11, 2020
e6dd8b2
pass instead of raising
jbrockmendel Feb 11, 2020
951a8d2
checkpoint passing
jbrockmendel Feb 11, 2020
65f10d0
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 11, 2020
49ec328
remove _convert_scalar_indexer
jbrockmendel Feb 11, 2020
eeb4c0e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 11, 2020
16edd93
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 12, 2020
22ccff9
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 13, 2020
458ed5c
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 13, 2020
7f2773b
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 15, 2020
9a01186
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 17, 2020
e91728f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 18, 2020
23290ec
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 28, 2020
4b4fda6
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Feb 29, 2020
e853160
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Mar 2, 2020
9dd6250
redundant check
jbrockmendel Mar 2, 2020
4397a8c
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Mar 3, 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
42 changes: 0 additions & 42 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3078,48 +3078,6 @@ def _get_partial_string_timestamp_match_key(self, key):
# GH#10331
return key

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

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

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

# we can raise here if we are definitive that this
# is positional indexing (eg. .loc on with a float)
# or label indexing if we are using a type able
# to be represented in the index

if kind == "getitem" and is_float(key):
if not self.is_floating():
raise KeyError(key)

elif kind == "loc" and is_float(key):

# we want to raise KeyError on string/mixed here
# technically we *could* raise a TypeError
# on anything but mixed though
if self.inferred_type not in [
"floating",
"mixed-integer-float",
"integer-na",
"string",
"mixed",
]:
raise KeyError(key)

elif kind == "loc" and is_integer(key):
if not (is_integer_dtype(self.dtype) or is_object_dtype(self.dtype)):
raise KeyError(key)

return key

def _validate_positional_slice(self, key: slice):
"""
For positional indexing, a slice must have either int or None
Expand Down
10 changes: 0 additions & 10 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,16 +574,6 @@ def get_indexer_non_unique(self, target):
indexer, missing = self._engine.get_indexer_non_unique(codes)
return ensure_platform_int(indexer), missing

@Appender(Index._convert_scalar_indexer.__doc__)
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="loc")
except TypeError:
raise KeyError(key)
return super()._convert_scalar_indexer(key, kind=kind)

@Appender(Index._convert_list_indexer.__doc__)
def _convert_list_indexer(self, keyarr):
# Return our indexer or raise if all of the values are not included in
Expand Down
27 changes: 0 additions & 27 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
is_bool_dtype,
is_categorical_dtype,
is_dtype_equal,
is_float,
is_integer,
is_list_like,
is_period_dtype,
Expand Down Expand Up @@ -377,32 +376,6 @@ def _format_attrs(self):
# --------------------------------------------------------------------
# Indexing Methods

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'}
"""
assert kind in ["loc", "getitem"]

if not is_scalar(key):
raise TypeError(key)

# we don't allow integer/float indexing for loc
# we don't allow float indexing for getitem
is_int = is_integer(key)
is_flt = is_float(key)
if kind == "loc" and (is_int or is_flt):
raise KeyError(key)
elif kind == "getitem" and is_flt:
raise KeyError(key)

return super()._convert_scalar_indexer(key, kind=kind)

def _validate_partial_date_slice(self, reso: str):
raise NotImplementedError

Expand Down
6 changes: 0 additions & 6 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,6 @@ def _should_fallback_to_positional(self):
# positional in this case
return self.dtype.subtype.kind in ["m", "M"]

@Appender(Index._convert_scalar_indexer.__doc__)
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):
return getattr(self, side)._maybe_cast_slice_bound(label, side, kind)

Expand Down
14 changes: 0 additions & 14 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,6 @@ def asi8(self) -> np.ndarray:
# do not cache or you'll create a memory leak
return self.values.view(self._default_dtype)

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

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


class Int64Index(IntegerIndex):
__doc__ = _num_index_shared_docs["class_descr"] % _int64_descr_args
Expand Down Expand Up @@ -391,12 +383,6 @@ def astype(self, dtype, copy=True):
def _should_fallback_to_positional(self):
return False

@Appender(Index._convert_scalar_indexer.__doc__)
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__)
def _convert_slice_indexer(self, key: slice, kind: str):
assert kind in ["loc", "getitem"]
Expand Down
34 changes: 11 additions & 23 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,16 +866,7 @@ def _validate_key(self, key, axis: int):
# slice of labels (where start-end in labels)
# slice of integers (only if in the labels)
# boolean

if isinstance(key, slice):
return

if com.is_bool_indexer(key):
return

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

def _has_valid_setitem_indexer(self, indexer) -> bool:
return True
Expand Down Expand Up @@ -1139,15 +1130,6 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
if isinstance(key, slice):
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 = labels._convert_scalar_indexer(key, kind="loc")
except KeyError:
# but we will allow setting
if not is_setter:
raise

# see if we are positional in nature
is_int_index = labels.is_integer()
is_int_positional = is_integer(key) and not is_int_index
Expand Down Expand Up @@ -2029,11 +2011,17 @@ def _convert_key(self, key, is_setter: bool = False):
if is_setter:
return list(key)

lkey = list(key)
for n, (ax, i) in enumerate(zip(self.obj.axes, key)):
lkey[n] = ax._convert_scalar_indexer(i, kind="loc")
return key

return tuple(lkey)
def __getitem__(self, key):
if self.ndim != 1 or not is_scalar(key):
# FIXME: is_scalar check is a kludge
return super().__getitem__(key)

# Like Index.get_value, but we do not allow positional fallback
obj = self.obj
loc = obj.index.get_loc(key)
return obj.index._get_values_for_loc(obj, loc, key)


@Appender(IndexingMixin.iat.__doc__)
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,7 @@ def __getitem__(self, key):
return self

key_is_scalar = is_scalar(key)
if key_is_scalar:
key = self.index._convert_scalar_indexer(key, kind="getitem")
elif isinstance(key, (list, tuple)):
if isinstance(key, (list, tuple)):
key = unpack_1tuple(key)

if key_is_scalar or isinstance(self.index, MultiIndex):
Expand Down Expand Up @@ -974,8 +972,6 @@ def _get_value(self, label, takeable: bool = False):

# Similar to Index.get_value, but we do not fall back to positional
loc = self.index.get_loc(label)
# We assume that _convert_scalar_indexer has already been called,
# with kind="loc", if necessary, by the time we get here
return self.index._get_values_for_loc(self, loc, label)

def __setitem__(self, key, value):
Expand Down