Skip to content

Commit ad58cf6

Browse files
authored
REF: do remove DTI._convert_arr_indexer (#41803)
1 parent e602f7b commit ad58cf6

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

pandas/core/indexes/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3428,7 +3428,7 @@ def get_indexer(
34283428
) -> np.ndarray:
34293429
# returned ndarray is np.intp
34303430
method = missing.clean_reindex_fill_method(method)
3431-
target = ensure_index(target)
3431+
target = self._maybe_cast_listlike_indexer(target)
34323432

34333433
self._check_indexing_method(method)
34343434

@@ -5678,6 +5678,12 @@ def _maybe_cast_indexer(self, key):
56785678
return com.cast_scalar_indexer(key)
56795679
return key
56805680

5681+
def _maybe_cast_listlike_indexer(self, target) -> Index:
5682+
"""
5683+
Analogue to maybe_cast_indexer for get_indexer instead of get_loc.
5684+
"""
5685+
return ensure_index(target)
5686+
56815687
@final
56825688
def _validate_indexer(self, form: str_t, key, kind: str_t):
56835689
"""

pandas/core/indexes/datetimelike.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
TimedeltaArray,
5050
)
5151
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
52+
import pandas.core.common as com
5253
import pandas.core.indexes.base as ibase
5354
from pandas.core.indexes.base import (
5455
Index,
@@ -593,12 +594,13 @@ def _from_join_target(self, result: np.ndarray):
593594

594595
# --------------------------------------------------------------------
595596

596-
@doc(Index._convert_arr_indexer)
597-
def _convert_arr_indexer(self, keyarr):
597+
@doc(Index._maybe_cast_listlike_indexer)
598+
def _maybe_cast_listlike_indexer(self, keyarr):
598599
try:
599-
return self._data._validate_listlike(keyarr, allow_object=True)
600+
res = self._data._validate_listlike(keyarr, allow_object=True)
600601
except (ValueError, TypeError):
601-
return super()._convert_arr_indexer(keyarr)
602+
res = com.asarray_tuplesafe(keyarr)
603+
return Index(res, dtype=res.dtype)
602604

603605

604606
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):

pandas/core/indexing.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
is_object_dtype,
3131
is_scalar,
3232
is_sequence,
33+
needs_i8_conversion,
3334
)
3435
from pandas.core.dtypes.concat import concat_compat
3536
from pandas.core.dtypes.generic import (
@@ -1301,10 +1302,19 @@ def _get_listlike_indexer(self, key, axis: int):
13011302

13021303
self._validate_read_indexer(keyarr, indexer, axis)
13031304

1304-
if isinstance(ax, (IntervalIndex, CategoricalIndex)):
1305-
# take instead of reindex to preserve dtype. For IntervalIndex
1306-
# this is to map integers to the Intervals they match to.
1305+
if needs_i8_conversion(ax.dtype) or isinstance(
1306+
ax, (IntervalIndex, CategoricalIndex)
1307+
):
1308+
# For CategoricalIndex take instead of reindex to preserve dtype.
1309+
# For IntervalIndex this is to map integers to the Intervals they match to.
13071310
keyarr = ax.take(indexer)
1311+
if keyarr.dtype.kind in ["m", "M"]:
1312+
# DTI/TDI.take can infer a freq in some cases when we dont want one
1313+
if isinstance(key, list) or (
1314+
isinstance(key, type(ax)) and key.freq is None
1315+
):
1316+
keyarr = keyarr._with_freq(None)
1317+
13081318
return keyarr, indexer
13091319

13101320
def _validate_read_indexer(self, key, indexer, axis: int):

pandas/tests/indexing/test_indexing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def test_getitem_ndarray_3d(
9494
msgs.append("Index data must be 1-dimensional")
9595
if isinstance(index, pd.IntervalIndex) and indexer_sli is tm.iloc:
9696
msgs.append("Index data must be 1-dimensional")
97+
if isinstance(index, (pd.TimedeltaIndex, pd.DatetimeIndex, pd.PeriodIndex)):
98+
msgs.append("Data must be 1-dimensional")
9799
if len(index) == 0 or isinstance(index, pd.MultiIndex):
98100
msgs.append("positional indexers are out-of-bounds")
99101
msg = "|".join(msgs)
@@ -127,6 +129,7 @@ def test_setitem_ndarray_3d(self, index, frame_or_series, indexer_sli):
127129
r"Buffer has wrong number of dimensions \(expected 1, got 3\)",
128130
"Cannot set values with ndim > 1",
129131
"Index data must be 1-dimensional",
132+
"Data must be 1-dimensional",
130133
"Array conditional must be same shape as self",
131134
]
132135
)

0 commit comments

Comments
 (0)