Skip to content

Commit d6a5a50

Browse files
committed
moved condition to maybe helper and added maybe downcast before comparison
1 parent d747141 commit d6a5a50

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

pandas/core/indexes/base.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,14 +3673,7 @@ def get_indexer(
36733673
method = clean_reindex_fill_method(method)
36743674
orig_target = target
36753675
target = self._maybe_cast_listlike_indexer(target)
3676-
3677-
from pandas.api.types import is_timedelta64_dtype
3678-
3679-
if target.dtype == "string[pyarrow]" and is_timedelta64_dtype(self.dtype):
3680-
from pandas.core.arrays.timedeltas import sequence_to_td64ns
3681-
3682-
data, freq = sequence_to_td64ns(target, copy=False, unit=None)
3683-
target = type(target)(data)
3676+
target, self = target._maybe_downcast_for_indexing(self)
36843677

36853678
self._check_indexing_method(method, limit, tolerance)
36863679

pandas/core/indexes/datetimelike.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,18 @@ def _maybe_cast_listlike_indexer(self, keyarr):
415415
# TODO: com.asarray_tuplesafe shouldn't cast e.g. DatetimeArray
416416
else:
417417
res = keyarr
418-
return Index(res, dtype=res.dtype)
418+
419+
res_index = Index(res, dtype=res.dtype)
420+
421+
if isinstance(res, ExtensionArray):
422+
from pandas.core.dtypes.common import is_timedelta64_dtype
423+
from pandas.core.arrays.timedeltas import sequence_to_td64ns
424+
425+
if res_index.dtype == "string[pyarrow]" and is_timedelta64_dtype(self.dtype):
426+
data, freq = sequence_to_td64ns(res_index, copy=False, unit=None)
427+
res_index = type(res_index)(data)
428+
429+
return res_index
419430

420431

421432
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, ABC):

pandas/core/indexes/datetimes.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,6 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
385385
# If we have tz, we can compare to tzaware
386386
return isinstance(dtype, DatetimeTZDtype)
387387

388-
from pandas import ArrowDtype
389-
390-
if isinstance(dtype, ArrowDtype):
391-
import pyarrow as pa
392-
393-
return (
394-
pa.types.is_date32(dtype.pyarrow_dtype)
395-
or pa.types.is_date64(dtype.pyarrow_dtype)
396-
or pa.types.is_timestamp(dtype.pyarrow_dtype)
397-
)
398-
399388
# if we dont have tz, we can only compare to tznaive
400389
return lib.is_np_dtype(dtype, "M")
401390

0 commit comments

Comments
 (0)