Skip to content

Commit 791c728

Browse files
author
MarcoGorelli
committed
reduce diff
1 parent d26e4e6 commit 791c728

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ Datetimelike
824824
- Bug in :func:`to_datetime` was giving incorrect results when using ``format='%Y%m%d'`` and ``errors='ignore'`` (:issue:`26493`)
825825
- Bug in :func:`to_datetime` was failing to parse date strings ``'today'`` and ``'now'`` if ``format`` was not ISO8601 (:issue:`50359`)
826826
- Bug in :func:`to_datetime` was raising ``ValueError`` when parsing mixed-offset :class:`Timestamp` with ``errors='ignore'`` (:issue:`50585`)
827-
- Bug in :func:`to_datetime` was not returning the input when parsing out-of-bounds ``np.datetime64`` with ``errors='ignore'`` (:issue:`50587`)
828827

829828
Timedelta
830829
^^^^^^^^^

pandas/_libs/tslib.pyx

+43
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ from pandas._libs.tslibs.np_datetime cimport (
3030
NPY_DATETIMEUNIT,
3131
NPY_FR_ns,
3232
check_dts_bounds,
33+
get_datetime64_value,
3334
npy_datetimestruct,
3435
npy_datetimestruct_to_datetime,
3536
pandas_datetime_to_datetimestruct,
@@ -630,6 +631,8 @@ cpdef array_to_datetime(
630631
continue
631632
elif is_raise:
632633
raise
634+
if isinstance(ex, OutOfBoundsDatetime):
635+
return ignore_errors_out_of_bounds_fallback(values), tz_out
633636
return values, None
634637

635638
except TypeError:
@@ -654,6 +657,46 @@ cpdef array_to_datetime(
654657
return result, tz_out
655658

656659

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+
657700
@cython.wraparound(False)
658701
@cython.boundscheck(False)
659702
cdef _array_to_datetime_object(

0 commit comments

Comments
 (0)