|
151 | 151 | Categorical,
|
152 | 152 | ExtensionArray,
|
153 | 153 | )
|
| 154 | +from pandas.core.arrays.datetimes import tz_to_dtype |
154 | 155 | from pandas.core.arrays.string_ import StringArray
|
155 | 156 | from pandas.core.base import (
|
156 | 157 | IndexOpsMixin,
|
|
191 | 192 | MultiIndex,
|
192 | 193 | Series,
|
193 | 194 | )
|
194 |
| - from pandas.core.arrays import PeriodArray |
195 |
| - |
| 195 | + from pandas.core.arrays import ( |
| 196 | + DatetimeArray, |
| 197 | + PeriodArray, |
| 198 | + TimedeltaArray, |
| 199 | + ) |
196 | 200 |
|
197 | 201 | __all__ = ["Index"]
|
198 | 202 |
|
@@ -835,6 +839,22 @@ def _engine(
|
835 | 839 | ) -> libindex.IndexEngine | libindex.ExtensionEngine | libindex.MaskedIndexEngine:
|
836 | 840 | # For base class (object dtype) we get ObjectEngine
|
837 | 841 | target_values = self._get_engine_target()
|
| 842 | + |
| 843 | + if isinstance(self._values, ArrowExtensionArray) and self.dtype.kind in "Mm": |
| 844 | + import pyarrow as pa |
| 845 | + |
| 846 | + pa_type = self._values._pa_array.type |
| 847 | + if pa.types.is_timestamp(pa_type): |
| 848 | + dtype = tz_to_dtype(pa_type.tz, pa_type.unit) |
| 849 | + target_values = self._values.astype(dtype) |
| 850 | + target_values = cast("DatetimeArray", target_values) |
| 851 | + return libindex.DatetimeEngine(target_values._ndarray) |
| 852 | + elif pa.types.is_duration(pa_type): |
| 853 | + dtype = np.dtype(f"m8[{pa_type.unit}]") |
| 854 | + target_values = self._values.astype(dtype) |
| 855 | + target_values = cast("TimedeltaArray", target_values) |
| 856 | + return libindex.TimedeltaEngine(target_values._ndarray) |
| 857 | + |
838 | 858 | if isinstance(target_values, ExtensionArray):
|
839 | 859 | if isinstance(target_values, (BaseMaskedArray, ArrowExtensionArray)):
|
840 | 860 | try:
|
@@ -5053,6 +5073,20 @@ def _get_engine_target(self) -> ArrayLike:
|
5053 | 5073 | if isinstance(vals, StringArray):
|
5054 | 5074 | # GH#45652 much more performant than ExtensionEngine
|
5055 | 5075 | return vals._ndarray
|
| 5076 | + if isinstance(vals, ArrowExtensionArray) and self.dtype.kind in "Mm": |
| 5077 | + import pyarrow as pa |
| 5078 | + |
| 5079 | + pa_type = vals._pa_array.type |
| 5080 | + if pa.types.is_timestamp(pa_type): |
| 5081 | + dtype = tz_to_dtype(pa_type.tz, pa_type.unit) |
| 5082 | + vals = vals.astype(dtype) |
| 5083 | + vals = cast("DatetimeArray", vals) |
| 5084 | + return vals._ndarray.view("i8") |
| 5085 | + elif pa.types.is_duration(pa_type): |
| 5086 | + dtype = np.dtype(f"m8[{pa_type.unit}]") |
| 5087 | + vals = vals.astype(dtype) |
| 5088 | + vals = cast("TimedeltaArray", vals) |
| 5089 | + return vals._ndarray.view("i8") |
5056 | 5090 | if (
|
5057 | 5091 | type(self) is Index
|
5058 | 5092 | and isinstance(self._values, ExtensionArray)
|
|
0 commit comments