@@ -30,6 +30,7 @@ from pandas._libs.tslibs.np_datetime cimport (
30
30
NPY_DATETIMEUNIT,
31
31
NPY_FR_ns,
32
32
check_dts_bounds,
33
+ get_datetime64_value,
33
34
npy_datetimestruct,
34
35
npy_datetimestruct_to_datetime,
35
36
pandas_datetime_to_datetimestruct,
@@ -630,6 +631,8 @@ cpdef array_to_datetime(
630
631
continue
631
632
elif is_raise:
632
633
raise
634
+ if isinstance (ex, OutOfBoundsDatetime):
635
+ return ignore_errors_out_of_bounds_fallback(values), tz_out
633
636
return values, None
634
637
635
638
except TypeError :
@@ -654,6 +657,46 @@ cpdef array_to_datetime(
654
657
return result, tz_out
655
658
656
659
660
+ @ cython.wraparound (False )
661
+ @ cython.boundscheck (False )
662
+ cdef ndarray[object ] ignore_errors_out_of_bounds_fallback(ndarray[object ] values):
663
+ """
664
+ Fallback for array_to_datetime if an OutOfBoundsDatetime is raised
665
+ and errors == "ignore"
666
+
667
+ Parameters
668
+ ----------
669
+ values : ndarray[object]
670
+
671
+ Returns
672
+ -------
673
+ ndarray[object]
674
+ """
675
+ cdef:
676
+ Py_ssize_t i, n = len (values)
677
+ object val
678
+
679
+ oresult = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0 )
680
+
681
+ for i in range (n):
682
+ val = values[i]
683
+
684
+ # set as nan except if its a NaT
685
+ if checknull_with_nat_and_na(val):
686
+ if isinstance (val, float ):
687
+ oresult[i] = np.nan
688
+ else :
689
+ oresult[i] = NaT
690
+ elif is_datetime64_object(val):
691
+ if get_datetime64_value(val) == NPY_NAT:
692
+ oresult[i] = NaT
693
+ else :
694
+ oresult[i] = val.item()
695
+ else :
696
+ oresult[i] = val
697
+ return oresult
698
+
699
+
657
700
@ cython.wraparound (False )
658
701
@ cython.boundscheck (False )
659
702
cdef _array_to_datetime_object(
0 commit comments