Skip to content

REF: remove Index._convert_list_indexer #41804

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
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: 1 addition & 17 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3720,7 +3720,7 @@ def _convert_listlike_indexer(self, keyarr):
else:
keyarr = self._convert_arr_indexer(keyarr)

indexer = self._convert_list_indexer(keyarr)
indexer = None
return indexer, keyarr

def _convert_arr_indexer(self, keyarr) -> np.ndarray:
Expand All @@ -3738,22 +3738,6 @@ def _convert_arr_indexer(self, keyarr) -> np.ndarray:
"""
return com.asarray_tuplesafe(keyarr)

def _convert_list_indexer(self, keyarr):
"""
Convert a list-like indexer to the appropriate dtype.

Parameters
----------
keyarr : Index (or sub-class)
Indexer to convert.
kind : iloc, loc, optional

Returns
-------
positional indexer or None
"""
return None

@final
def _invalid_indexer(self, form: str_t, key) -> TypeError:
"""
Expand Down
12 changes: 0 additions & 12 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,18 +525,6 @@ def _get_indexer_non_unique(
indexer, missing = self._engine.get_indexer_non_unique(codes)
return ensure_platform_int(indexer), ensure_platform_int(missing)

@doc(Index._convert_list_indexer)
def _convert_list_indexer(self, keyarr):
# Return our indexer or raise if all of the values are not included in
# the categories

if self.categories._defer_to_indexing:
# See tests.indexing.interval.test_interval:test_loc_getitem_frame
indexer = self.categories._convert_list_indexer(keyarr)
return Index(self.codes).get_indexer_for(indexer)

return self.get_indexer_for(keyarr)

# --------------------------------------------------------------------

def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
Expand Down
14 changes: 0 additions & 14 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,20 +815,6 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
return getattr(self, side)._maybe_cast_slice_bound(label, side)

@Appender(Index._convert_list_indexer.__doc__)
def _convert_list_indexer(self, keyarr):
"""
we are passed a list-like indexer. Return the
indexer for matching intervals.
"""
locs = self.get_indexer_for(keyarr)

# we have missing values
if (locs == -1).any():
raise KeyError(keyarr[locs == -1].tolist())

return locs

def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
if not isinstance(dtype, IntervalDtype):
return False
Expand Down
23 changes: 20 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
length_of_indexer,
)
from pandas.core.indexes.api import (
CategoricalIndex,
Index,
IntervalIndex,
MultiIndex,
ensure_index,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -1297,6 +1300,11 @@ def _get_listlike_indexer(self, key, axis: int):
keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)

self._validate_read_indexer(keyarr, indexer, axis)

if isinstance(ax, (IntervalIndex, CategoricalIndex)):
# take instead of reindex to preserve dtype. For IntervalIndex
# this is to map integers to the Intervals they match to.
keyarr = ax.take(indexer)
return keyarr, indexer

def _validate_read_indexer(self, key, indexer, axis: int):
Expand Down Expand Up @@ -1329,13 +1337,22 @@ def _validate_read_indexer(self, key, indexer, axis: int):
missing = (missing_mask).sum()

if missing:
ax = self.obj._get_axis(axis)

# TODO: remove special-case; this is just to keep exception
# message tests from raising while debugging
use_interval_msg = isinstance(ax, IntervalIndex) or (
isinstance(ax, CategoricalIndex)
and isinstance(ax.categories, IntervalIndex)
)

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

ax = self.obj._get_axis(axis)

not_found = list(set(key) - set(ax))
not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
raise KeyError(f"{not_found} not in index")


Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexing/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def test_getitem_non_matching(self, series_with_interval_index, indexer_sl):

# this is a departure from our current
# indexing scheme, but simpler
with pytest.raises(KeyError, match=r"^\[-1\]$"):
with pytest.raises(KeyError, match=r"\[-1\] not in index"):
indexer_sl(ser)[[-1, 3, 4, 5]]

with pytest.raises(KeyError, match=r"^\[-1\]$"):
with pytest.raises(KeyError, match=r"\[-1\] not in index"):
indexer_sl(ser)[[-1, 3]]

@pytest.mark.slow
Expand Down Expand Up @@ -107,11 +107,11 @@ def test_loc_getitem_frame(self):
expected = df.take([4, 5, 4, 5])
tm.assert_frame_equal(result, expected)

with pytest.raises(KeyError, match=r"^\[10\]$"):
with pytest.raises(KeyError, match=r"None of \[\[10\]\] are"):
df.loc[[10]]

# partial missing
with pytest.raises(KeyError, match=r"^\[10\]$"):
with pytest.raises(KeyError, match=r"\[10\] not in index"):
df.loc[[10, 4]]


Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexing/interval/test_interval_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ def test_loc_with_overlap(self, indexer_sl):
with pytest.raises(KeyError, match=re.escape("Interval(3, 5, closed='right')")):
indexer_sl(ser)[Interval(3, 5)]

with pytest.raises(KeyError, match=r"^\[Interval\(3, 5, closed='right'\)\]$"):
msg = r"None of \[\[Interval\(3, 5, closed='right'\)\]\]"
with pytest.raises(KeyError, match=msg):
indexer_sl(ser)[[Interval(3, 5)]]

# slices with interval (only exact matches)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def test_multitype_list_index_access(self):
# GH 10610
df = DataFrame(np.random.random((10, 5)), columns=["a"] + [20, 21, 22, 23])

with pytest.raises(KeyError, match=re.escape("'[-8, 26] not in index'")):
with pytest.raises(KeyError, match=re.escape("'[26, -8] not in index'")):
df[[22, 26, -8]]
assert df[21].shape[0] == df.shape[0]

Expand Down