Skip to content

Commit 24a9073

Browse files
BUG: to_datetime with out-of-bounds np.datetime64 and errors='ignore' doesn't return input (#50992)
* fixup ignore inconsistency * add unit test, clarify whatsnew * simplify Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: MarcoGorelli <> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent d1fc79d commit 24a9073

File tree

3 files changed

+8
-61
lines changed

3 files changed

+8
-61
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ Datetimelike
11021102
- Bug in :func:`to_datetime` was raising ``ValueError`` when parsing mixed-offset :class:`Timestamp` with ``errors='ignore'`` (:issue:`50585`)
11031103
- Bug in :func:`to_datetime` was incorrectly handling floating-point inputs within 1 ``unit`` of the overflow boundaries (:issue:`50183`)
11041104
- Bug in :func:`to_datetime` with unit of "Y" or "M" giving incorrect results, not matching pointwise :class:`Timestamp` results (:issue:`50870`)
1105+
- Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`)
11051106
-
11061107

11071108
Timedelta

pandas/_libs/tslib.pyx

-60
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ from pandas._libs.tslibs.np_datetime cimport (
3434
NPY_DATETIMEUNIT,
3535
NPY_FR_ns,
3636
check_dts_bounds,
37-
get_datetime64_value,
3837
npy_datetimestruct,
3938
npy_datetimestruct_to_datetime,
4039
pandas_datetime_to_datetimestruct,
@@ -542,16 +541,6 @@ cpdef array_to_datetime(
542541

543542
cnp.PyArray_MultiIter_NEXT(mi)
544543

545-
except OutOfBoundsDatetime as ex:
546-
ex.args = (f"{ex}, at position {i}",)
547-
if is_coerce:
548-
iresult[i] = NPY_NAT
549-
cnp.PyArray_MultiIter_NEXT(mi)
550-
continue
551-
elif is_raise:
552-
raise
553-
return ignore_errors_out_of_bounds_fallback(values), tz_out
554-
555544
except (TypeError, OverflowError, ValueError) as ex:
556545
ex.args = (f"{ex}, at position {i}",)
557546
if is_coerce:
@@ -578,55 +567,6 @@ cpdef array_to_datetime(
578567
return result, tz_out
579568

580569

581-
@cython.wraparound(False)
582-
@cython.boundscheck(False)
583-
cdef ndarray ignore_errors_out_of_bounds_fallback(ndarray values):
584-
"""
585-
Fallback for array_to_datetime if an OutOfBoundsDatetime is raised
586-
and errors == "ignore"
587-
588-
Parameters
589-
----------
590-
values : ndarray[object]
591-
592-
Returns
593-
-------
594-
ndarray[object]
595-
"""
596-
cdef:
597-
Py_ssize_t i, n = values.size
598-
object val
599-
cnp.broadcast mi
600-
ndarray[object] oresult
601-
ndarray oresult_nd
602-
603-
oresult_nd = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)
604-
mi = cnp.PyArray_MultiIterNew2(oresult_nd, values)
605-
oresult = oresult_nd.ravel()
606-
607-
for i in range(n):
608-
# Analogous to `val = values[i]`
609-
val = <object>(<PyObject**>cnp.PyArray_MultiIter_DATA(mi, 1))[0]
610-
611-
# set as nan except if its a NaT
612-
if checknull_with_nat_and_na(val):
613-
if isinstance(val, float):
614-
oresult[i] = np.nan
615-
else:
616-
oresult[i] = <object>NaT
617-
elif is_datetime64_object(val):
618-
if get_datetime64_value(val) == NPY_NAT:
619-
oresult[i] = <object>NaT
620-
else:
621-
oresult[i] = val.item()
622-
else:
623-
oresult[i] = val
624-
625-
cnp.PyArray_MultiIter_NEXT(mi)
626-
627-
return oresult
628-
629-
630570
@cython.wraparound(False)
631571
@cython.boundscheck(False)
632572
cdef _array_to_datetime_object(

pandas/tests/tools/test_to_datetime.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,15 @@ def test_to_datetime_array_of_dt64s(self, cache, unit):
10841084
# numpy is either a python datetime.datetime or datetime.date
10851085
tm.assert_index_equal(
10861086
to_datetime(dts_with_oob, errors="ignore", cache=cache),
1087-
Index([dt.item() for dt in dts_with_oob]),
1087+
Index(dts_with_oob),
10881088
)
10891089

1090+
def test_out_of_bounds_errors_ignore(self):
1091+
# https://github.com/pandas-dev/pandas/issues/50587
1092+
result = to_datetime(np.datetime64("9999-01-01"), errors="ignore")
1093+
expected = np.datetime64("9999-01-01")
1094+
assert result == expected
1095+
10901096
def test_to_datetime_tz(self, cache):
10911097

10921098
# xref 8260

0 commit comments

Comments
 (0)