@@ -96,6 +96,9 @@ from pandas._libs.missing cimport (
96
96
is_null_datetime64,
97
97
is_null_timedelta64,
98
98
)
99
+ from pandas._libs.tslibs.timestamps import Timestamp
100
+ from pandas._libs.tslibs.timedeltas import Timedelta
101
+
99
102
from pandas._libs.tslibs.conversion cimport convert_to_tsobject
100
103
from pandas._libs.tslibs.nattype cimport (
101
104
NPY_NAT,
@@ -2615,7 +2618,10 @@ def maybe_convert_objects(ndarray[object] objects,
2615
2618
else :
2616
2619
seen.object_ = True
2617
2620
break
2618
- elif PyDate_Check(val):
2621
+ elif (
2622
+ PyDate_Check(val)
2623
+ or (pa is not None and isinstance (val, (pa.Date32Scalar, pa.Date64Scalar)))
2624
+ ):
2619
2625
if convert_non_numeric:
2620
2626
seen.date_ = True
2621
2627
break
@@ -2668,12 +2674,16 @@ def maybe_convert_objects(ndarray[object] objects,
2668
2674
if storage == " pyarrow" :
2669
2675
from pandas.core.dtypes.dtypes import ArrowDtype
2670
2676
2677
+ datetime64_array = None
2671
2678
if isinstance (val, datetime):
2672
2679
objects[mask] = None
2680
+ datetime64_array = objects.astype(Timestamp)
2673
2681
else :
2674
2682
objects[mask] = np.datetime64(" NaT" )
2675
- datetime64_array = objects.astype(val.dtype)
2676
- pa_array = pa.array(datetime64_array)
2683
+ datetime64_array = objects.astype(val.dtype)
2684
+ pa_array = pa.array(datetime64_array).cast(
2685
+ pa.timestamp(val.resolution.unit, val.tzinfo)
2686
+ )
2677
2687
dtype = ArrowDtype(pa_array.type)
2678
2688
return dtype.construct_array_type()._from_sequence(pa_array, dtype = dtype)
2679
2689
@@ -2727,17 +2737,32 @@ def maybe_convert_objects(ndarray[object] objects,
2727
2737
return dtype.construct_array_type()._from_sequence(pa_array, dtype = dtype)
2728
2738
2729
2739
elif seen.timedelta_:
2730
- if is_timedelta_or_timedelta64_array(objects) :
2731
- from pandas import TimedeltaIndex
2740
+ if storage == " pyarrow " :
2741
+ from pandas.core.dtypes.dtypes import ArrowDtype
2732
2742
2733
- try :
2734
- tdi = TimedeltaIndex(objects)
2735
- except OutOfBoundsTimedelta:
2736
- pass
2743
+ timedelta64_array = None
2744
+ if isinstance (val, timedelta):
2745
+ objects[mask] = None
2746
+ timedelta64_array = objects.astype(Timedelta)
2737
2747
else :
2738
- # unbox to ndarray[timedelta64[ns]]
2739
- return tdi._data._ndarray
2740
- seen.object_ = True
2748
+ objects[mask] = np.timedelta64(" NaT" )
2749
+ timedelta64_array = objects.astype(val.dtype)
2750
+ pa_array = pa.array(timedelta64_array)
2751
+
2752
+ dtype = ArrowDtype(pa_array.type)
2753
+ return dtype.construct_array_type()._from_sequence(pa_array, dtype = dtype)
2754
+ else :
2755
+ if is_timedelta_or_timedelta64_array(objects):
2756
+ from pandas import TimedeltaIndex
2757
+
2758
+ try :
2759
+ tdi = TimedeltaIndex(objects)
2760
+ except OutOfBoundsTimedelta:
2761
+ pass
2762
+ else :
2763
+ # unbox to ndarray[timedelta64[ns]]
2764
+ return tdi._data._ndarray
2765
+ seen.object_ = True
2741
2766
2742
2767
elif seen.period_:
2743
2768
if is_period_array(objects):
0 commit comments