Skip to content

REF: do remove DTI._convert_arr_indexer #41803

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 4 commits into from
Jun 4, 2021
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
8 changes: 7 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3428,7 +3428,7 @@ def get_indexer(
) -> np.ndarray:
# returned ndarray is np.intp
method = missing.clean_reindex_fill_method(method)
target = ensure_index(target)
target = self._maybe_cast_listlike_indexer(target)

self._check_indexing_method(method)

Expand Down Expand Up @@ -5678,6 +5678,12 @@ def _maybe_cast_indexer(self, key):
return com.cast_scalar_indexer(key)
return key

def _maybe_cast_listlike_indexer(self, target) -> Index:
"""
Analogue to maybe_cast_indexer for get_indexer instead of get_loc.
"""
return ensure_index(target)

@final
def _validate_indexer(self, form: str_t, key, kind: str_t):
"""
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
TimedeltaArray,
)
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
import pandas.core.common as com
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import (
Index,
Expand Down Expand Up @@ -593,12 +594,13 @@ def _from_join_target(self, result: np.ndarray):

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

@doc(Index._convert_arr_indexer)
def _convert_arr_indexer(self, keyarr):
@doc(Index._maybe_cast_listlike_indexer)
def _maybe_cast_listlike_indexer(self, keyarr):
try:
return self._data._validate_listlike(keyarr, allow_object=True)
res = self._data._validate_listlike(keyarr, allow_object=True)
except (ValueError, TypeError):
return super()._convert_arr_indexer(keyarr)
res = com.asarray_tuplesafe(keyarr)
return Index(res, dtype=res.dtype)


class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):
Expand Down
16 changes: 13 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
is_object_dtype,
is_scalar,
is_sequence,
needs_i8_conversion,
)
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.generic import (
Expand Down Expand Up @@ -1301,10 +1302,19 @@ def _get_listlike_indexer(self, key, axis: int):

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.
if needs_i8_conversion(ax.dtype) or isinstance(
ax, (IntervalIndex, CategoricalIndex)
):
# For 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)
if keyarr.dtype.kind in ["m", "M"]:
# DTI/TDI.take can infer a freq in some cases when we dont want one
if isinstance(key, list) or (
isinstance(key, type(ax)) and key.freq is None
):
keyarr = keyarr._with_freq(None)

return keyarr, indexer

def _validate_read_indexer(self, key, indexer, axis: int):
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def test_getitem_ndarray_3d(
msgs.append("Index data must be 1-dimensional")
if isinstance(index, pd.IntervalIndex) and indexer_sli is tm.iloc:
msgs.append("Index data must be 1-dimensional")
if isinstance(index, (pd.TimedeltaIndex, pd.DatetimeIndex, pd.PeriodIndex)):
msgs.append("Data must be 1-dimensional")
if len(index) == 0 or isinstance(index, pd.MultiIndex):
msgs.append("positional indexers are out-of-bounds")
msg = "|".join(msgs)
Expand Down Expand Up @@ -127,6 +129,7 @@ def test_setitem_ndarray_3d(self, index, frame_or_series, indexer_sli):
r"Buffer has wrong number of dimensions \(expected 1, got 3\)",
"Cannot set values with ndim > 1",
"Index data must be 1-dimensional",
"Data must be 1-dimensional",
"Array conditional must be same shape as self",
]
)
Expand Down