Skip to content

REF: make _convert_scalar_indexer require a scalar #31676

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 13 commits into from
Feb 5, 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
18 changes: 10 additions & 8 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,17 @@ def _convert_scalar_indexer(self, key, kind=None):

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

if not is_scalar(key):
raise TypeError(key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this now caught by?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check in indexing L1800 prevents us from actually getting here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk


# we don't allow integer/float indexing for loc
# we don't allow float indexing for ix/getitem
if is_scalar(key):
is_int = is_integer(key)
is_flt = is_float(key)
if kind in ["loc"] and (is_int or is_flt):
self._invalid_indexer("index", key)
elif kind in ["getitem"] and is_flt:
self._invalid_indexer("index", key)
# 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):
self._invalid_indexer("index", key)
elif kind == "getitem" and is_flt:
self._invalid_indexer("index", key)

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

Expand Down
34 changes: 19 additions & 15 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def _slice(self, obj, axis: int, kind=None):

def _get_setitem_indexer(self, key):
if self.axis is not None:
return self._convert_tuple(key)
return self._convert_tuple(key, setting=True)

ax = self.obj._get_axis(0)

Expand All @@ -612,15 +612,15 @@ def _get_setitem_indexer(self, key):

if isinstance(key, tuple):
try:
return self._convert_tuple(key)
return self._convert_tuple(key, setting=True)
except IndexingError:
pass

if isinstance(key, range):
return list(key)

try:
return self._convert_to_indexer(key, axis=0)
return self._convert_to_indexer(key, axis=0, setting=True)
except TypeError as e:

# invalid indexer type vs 'other' indexing errors
Expand Down Expand Up @@ -683,20 +683,22 @@ def _is_nested_tuple_indexer(self, tup: Tuple) -> bool:
return any(is_nested_tuple(tup, ax) for ax in self.obj.axes)
return False

def _convert_tuple(self, key):
def _convert_tuple(self, key, setting: bool = False):
keyidx = []
if self.axis is not None:
axis = self.obj._get_axis_number(self.axis)
for i in range(self.ndim):
if i == axis:
keyidx.append(self._convert_to_indexer(key, axis=axis))
keyidx.append(
self._convert_to_indexer(key, axis=axis, setting=setting)
)
else:
keyidx.append(slice(None))
else:
for i, k in enumerate(key):
if i >= self.ndim:
raise IndexingError("Too many indexers")
idx = self._convert_to_indexer(k, axis=i)
idx = self._convert_to_indexer(k, axis=i, setting=setting)
keyidx.append(idx)
return tuple(keyidx)

Expand Down Expand Up @@ -1566,7 +1568,7 @@ def _validate_read_indexer(
"https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
)

def _convert_to_indexer(self, key, axis: int):
def _convert_to_indexer(self, key, axis: int, setting: bool = False):
raise AbstractMethodError(self)

def __getitem__(self, key):
Expand Down Expand Up @@ -1775,7 +1777,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int):
# return a DatetimeIndex instead of a slice object.
return self.obj.take(indexer, axis=axis)

def _convert_to_indexer(self, key, axis: int):
def _convert_to_indexer(self, key, axis: int, setting: bool = False):
"""
Convert indexing key into something we can use to do actual fancy
indexing on a ndarray.
Expand All @@ -1795,12 +1797,14 @@ def _convert_to_indexer(self, key, axis: int):
if isinstance(key, slice):
return self._convert_slice_indexer(key, axis)

# try to find out correct indexer, if not type correct raise
try:
key = self._convert_scalar_indexer(key, axis)
except TypeError:
# but we will allow setting
pass
if is_scalar(key):
# try to find out correct indexer, if not type correct raise
try:
key = self._convert_scalar_indexer(key, axis)
except TypeError:
# but we will allow setting
if not setting:
raise

# see if we are positional in nature
is_int_index = labels.is_integer()
Expand Down Expand Up @@ -2032,7 +2036,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int):
indexer = self._convert_slice_indexer(slice_obj, axis)
return self._slice(indexer, axis=axis, kind="iloc")

def _convert_to_indexer(self, key, axis: int):
def _convert_to_indexer(self, key, axis: int, setting: bool = False):
"""
Much simpler as we only have to deal with our valid types.
"""
Expand Down