Skip to content

Commit 625441b

Browse files
authored
REF: move loc-only validate_read_indexer to Loc (#31834)
1 parent 81669e3 commit 625441b

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

pandas/core/indexing.py

+60-60
Original file line numberDiff line numberDiff line change
@@ -1301,66 +1301,6 @@ def _getitem_nested_tuple(self, tup: Tuple):
13011301

13021302
return obj
13031303

1304-
def _validate_read_indexer(
1305-
self, key, indexer, axis: int, raise_missing: bool = False
1306-
):
1307-
"""
1308-
Check that indexer can be used to return a result.
1309-
1310-
e.g. at least one element was found,
1311-
unless the list of keys was actually empty.
1312-
1313-
Parameters
1314-
----------
1315-
key : list-like
1316-
Targeted labels (only used to show correct error message).
1317-
indexer: array-like of booleans
1318-
Indices corresponding to the key,
1319-
(with -1 indicating not found).
1320-
axis: int
1321-
Dimension on which the indexing is being made.
1322-
raise_missing: bool
1323-
Whether to raise a KeyError if some labels are not found. Will be
1324-
removed in the future, and then this method will always behave as
1325-
if raise_missing=True.
1326-
1327-
Raises
1328-
------
1329-
KeyError
1330-
If at least one key was requested but none was found, and
1331-
raise_missing=True.
1332-
"""
1333-
ax = self.obj._get_axis(axis)
1334-
1335-
if len(key) == 0:
1336-
return
1337-
1338-
# Count missing values:
1339-
missing = (indexer < 0).sum()
1340-
1341-
if missing:
1342-
if missing == len(indexer):
1343-
axis_name = self.obj._get_axis_name(axis)
1344-
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
1345-
1346-
# We (temporarily) allow for some missing keys with .loc, except in
1347-
# some cases (e.g. setting) in which "raise_missing" will be False
1348-
if not (self.name == "loc" and not raise_missing):
1349-
not_found = list(set(key) - set(ax))
1350-
raise KeyError(f"{not_found} not in index")
1351-
1352-
# we skip the warning on Categorical/Interval
1353-
# as this check is actually done (check for
1354-
# non-missing values), but a bit later in the
1355-
# code, so we want to avoid warning & then
1356-
# just raising
1357-
if not (ax.is_categorical() or ax.is_interval()):
1358-
raise KeyError(
1359-
"Passing list-likes to .loc or [] with any missing labels "
1360-
"is no longer supported, see "
1361-
"https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
1362-
)
1363-
13641304
def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
13651305
raise AbstractMethodError(self)
13661306

@@ -1819,6 +1759,66 @@ def _get_listlike_indexer(self, key, axis: int, raise_missing: bool = False):
18191759
self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
18201760
return keyarr, indexer
18211761

1762+
def _validate_read_indexer(
1763+
self, key, indexer, axis: int, raise_missing: bool = False
1764+
):
1765+
"""
1766+
Check that indexer can be used to return a result.
1767+
1768+
e.g. at least one element was found,
1769+
unless the list of keys was actually empty.
1770+
1771+
Parameters
1772+
----------
1773+
key : list-like
1774+
Targeted labels (only used to show correct error message).
1775+
indexer: array-like of booleans
1776+
Indices corresponding to the key,
1777+
(with -1 indicating not found).
1778+
axis: int
1779+
Dimension on which the indexing is being made.
1780+
raise_missing: bool
1781+
Whether to raise a KeyError if some labels are not found. Will be
1782+
removed in the future, and then this method will always behave as
1783+
if raise_missing=True.
1784+
1785+
Raises
1786+
------
1787+
KeyError
1788+
If at least one key was requested but none was found, and
1789+
raise_missing=True.
1790+
"""
1791+
ax = self.obj._get_axis(axis)
1792+
1793+
if len(key) == 0:
1794+
return
1795+
1796+
# Count missing values:
1797+
missing = (indexer < 0).sum()
1798+
1799+
if missing:
1800+
if missing == len(indexer):
1801+
axis_name = self.obj._get_axis_name(axis)
1802+
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
1803+
1804+
# We (temporarily) allow for some missing keys with .loc, except in
1805+
# some cases (e.g. setting) in which "raise_missing" will be False
1806+
if not (self.name == "loc" and not raise_missing):
1807+
not_found = list(set(key) - set(ax))
1808+
raise KeyError(f"{not_found} not in index")
1809+
1810+
# we skip the warning on Categorical/Interval
1811+
# as this check is actually done (check for
1812+
# non-missing values), but a bit later in the
1813+
# code, so we want to avoid warning & then
1814+
# just raising
1815+
if not (ax.is_categorical() or ax.is_interval()):
1816+
raise KeyError(
1817+
"Passing list-likes to .loc or [] with any missing labels "
1818+
"is no longer supported, see "
1819+
"https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
1820+
)
1821+
18221822

18231823
@Appender(IndexingMixin.iloc.__doc__)
18241824
class _iLocIndexer(_LocationIndexer):

0 commit comments

Comments
 (0)