Skip to content

Commit 031fbc7

Browse files
author
mcortesdf
committed
Fix to_datetime(errors='coerce') not swallowing all parser exceptions (pandas-dev#28299)
1 parent 2d65e38 commit 031fbc7

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

doc/source/whatsnew/v0.25.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Datetimelike
3030
- Bug in :func:`to_datetime` where passing a timezone-naive :class:`DatetimeArray` or :class:`DatetimeIndex` and ``utc=True`` would incorrectly return a timezone-naive result (:issue:`27733`)
3131
- Bug in :meth:`Period.to_timestamp` where a :class:`Period` outside the :class:`Timestamp` implementation bounds (roughly 1677-09-21 to 2262-04-11) would return an incorrect :class:`Timestamp` instead of raising ``OutOfBoundsDatetime`` (:issue:`19643`)
3232
- Bug in iterating over :class:`DatetimeIndex` when the underlying data is read-only (:issue:`28055`)
33+
- Bug in :func:`to_datetime` where passing arrays of malformed ``str`` while attempting to coerce errors could incorrectly lead to raising errors instead (:issue:`28299`)
3334

3435
Timezones
3536
^^^^^^^^^

pandas/_libs/tslib.pyx

+4-3
Original file line numberDiff line numberDiff line change
@@ -610,16 +610,17 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
610610
py_dt = parse_datetime_string(val,
611611
dayfirst=dayfirst,
612612
yearfirst=yearfirst)
613+
# If the dateutil parser returned tzinfo, capture it
614+
# to check if all arguments have the same tzinfo
615+
tz = py_dt.utcoffset()
616+
613617
except Exception:
614618
if is_coerce:
615619
iresult[i] = NPY_NAT
616620
continue
617621
raise TypeError("invalid string coercion to "
618622
"datetime")
619623

620-
# If the dateutil parser returned tzinfo, capture it
621-
# to check if all arguments have the same tzinfo
622-
tz = py_dt.utcoffset()
623624
if tz is not None:
624625
seen_datetime_offset = 1
625626
# dateutil timezone objects cannot be hashed, so

pandas/tests/indexes/datetimes/test_tools.py

+7
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,13 @@ def test_to_datetime_coerce(self):
901901
)
902902
tm.assert_index_equal(result, expected)
903903

904+
def test_to_datetime_coerce_malformed(self):
905+
# GH 28299
906+
ts_strings = ["200622-12-31", "111111-24-11"]
907+
result = to_datetime(ts_strings, errors="coerce")
908+
expected = Index([NaT, NaT])
909+
tm.assert_index_equal(result, expected)
910+
904911
def test_iso_8601_strings_with_same_offset(self):
905912
# GH 17697, 11736
906913
ts_str = "2015-11-18 15:30:00+05:30"

0 commit comments

Comments
 (0)