|
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 |
|
@@ -826,6 +830,22 @@ def _engine(
|
826 | 830 | ) -> libindex.IndexEngine | libindex.ExtensionEngine | libindex.MaskedIndexEngine:
|
827 | 831 | # For base class (object dtype) we get ObjectEngine
|
828 | 832 | target_values = self._get_engine_target()
|
| 833 | + |
| 834 | + if isinstance(self._values, ArrowExtensionArray) and self.dtype.kind in "Mm": |
| 835 | + import pyarrow as pa |
| 836 | + |
| 837 | + pa_type = self._values._pa_array.type |
| 838 | + if pa.types.is_timestamp(pa_type): |
| 839 | + dtype = tz_to_dtype(pa_type.tz, pa_type.unit) |
| 840 | + target_values = self._values.astype(dtype) |
| 841 | + target_values = cast("DatetimeArray", target_values) |
| 842 | + return libindex.DatetimeEngine(target_values._ndarray) |
| 843 | + elif pa.types.is_duration(pa_type): |
| 844 | + dtype = np.dtype(f"m8[{pa_type.unit}]") |
| 845 | + target_values = self._values.astype(dtype) |
| 846 | + target_values = cast("TimedeltaArray", target_values) |
| 847 | + return libindex.TimedeltaEngine(target_values._ndarray) |
| 848 | + |
829 | 849 | if isinstance(target_values, ExtensionArray):
|
830 | 850 | if isinstance(target_values, (BaseMaskedArray, ArrowExtensionArray)):
|
831 | 851 | try:
|
@@ -5044,6 +5064,20 @@ def _get_engine_target(self) -> ArrayLike:
|
5044 | 5064 | if isinstance(vals, StringArray):
|
5045 | 5065 | # GH#45652 much more performant than ExtensionEngine
|
5046 | 5066 | return vals._ndarray
|
| 5067 | + if isinstance(vals, ArrowExtensionArray) and self.dtype.kind in "Mm": |
| 5068 | + import pyarrow as pa |
| 5069 | + |
| 5070 | + pa_type = vals._pa_array.type |
| 5071 | + if pa.types.is_timestamp(pa_type): |
| 5072 | + dtype = tz_to_dtype(pa_type.tz, pa_type.unit) |
| 5073 | + vals = vals.astype(dtype) |
| 5074 | + vals = cast("DatetimeArray", vals) |
| 5075 | + return vals._ndarray.view("i8") |
| 5076 | + elif pa.types.is_duration(pa_type): |
| 5077 | + dtype = np.dtype(f"m8[{pa_type.unit}]") |
| 5078 | + vals = vals.astype(dtype) |
| 5079 | + vals = cast("TimedeltaArray", vals) |
| 5080 | + return vals._ndarray.view("i8") |
5047 | 5081 | if (
|
5048 | 5082 | type(self) is Index
|
5049 | 5083 | and isinstance(self._values, ExtensionArray)
|
|
0 commit comments