Skip to content

REF: move loc-only validate_read_indexer to Loc #31834

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 1 commit into from
Feb 9, 2020
Merged
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
120 changes: 60 additions & 60 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,66 +1304,6 @@ def _getitem_nested_tuple(self, tup: Tuple):

return obj

def _validate_read_indexer(
self, key, indexer, axis: int, raise_missing: bool = False
):
"""
Check that indexer can be used to return a result.

e.g. at least one element was found,
unless the list of keys was actually empty.

Parameters
----------
key : list-like
Targeted labels (only used to show correct error message).
indexer: array-like of booleans
Indices corresponding to the key,
(with -1 indicating not found).
axis: int
Dimension on which the indexing is being made.
raise_missing: bool
Whether to raise a KeyError if some labels are not found. Will be
removed in the future, and then this method will always behave as
if raise_missing=True.

Raises
------
KeyError
If at least one key was requested but none was found, and
raise_missing=True.
"""
ax = self.obj._get_axis(axis)

if len(key) == 0:
return

# Count missing values:
missing = (indexer < 0).sum()

if missing:
if missing == len(indexer):
axis_name = self.obj._get_axis_name(axis)
raise KeyError(f"None of [{key}] are in the [{axis_name}]")

# 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):
not_found = list(set(key) - set(ax))
raise KeyError(f"{not_found} not in index")

# we skip the warning on Categorical/Interval
# as this check is actually done (check for
# non-missing values), but a bit later in the
# code, so we want to avoid warning & then
# just raising
if not (ax.is_categorical() or ax.is_interval()):
raise KeyError(
"Passing list-likes to .loc or [] with any missing labels "
"is no longer supported, see "
"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, is_setter: bool = False):
raise AbstractMethodError(self)

Expand Down Expand Up @@ -1822,6 +1762,66 @@ def _get_listlike_indexer(self, key, axis: int, raise_missing: bool = False):
self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
return keyarr, indexer

def _validate_read_indexer(
self, key, indexer, axis: int, raise_missing: bool = False
):
"""
Check that indexer can be used to return a result.

e.g. at least one element was found,
unless the list of keys was actually empty.

Parameters
----------
key : list-like
Targeted labels (only used to show correct error message).
indexer: array-like of booleans
Indices corresponding to the key,
(with -1 indicating not found).
axis: int
Dimension on which the indexing is being made.
raise_missing: bool
Whether to raise a KeyError if some labels are not found. Will be
removed in the future, and then this method will always behave as
if raise_missing=True.

Raises
------
KeyError
If at least one key was requested but none was found, and
raise_missing=True.
"""
ax = self.obj._get_axis(axis)

if len(key) == 0:
return

# Count missing values:
missing = (indexer < 0).sum()

if missing:
if missing == len(indexer):
axis_name = self.obj._get_axis_name(axis)
raise KeyError(f"None of [{key}] are in the [{axis_name}]")

# 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):
not_found = list(set(key) - set(ax))
raise KeyError(f"{not_found} not in index")

# we skip the warning on Categorical/Interval
# as this check is actually done (check for
# non-missing values), but a bit later in the
# code, so we want to avoid warning & then
# just raising
if not (ax.is_categorical() or ax.is_interval()):
raise KeyError(
"Passing list-likes to .loc or [] with any missing labels "
"is no longer supported, see "
"https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
)


@Appender(IndexingMixin.iloc.__doc__)
class _iLocIndexer(_LocationIndexer):
Expand Down