Skip to content

Commit 415c806

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

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pandas/_libs/tslib.pyx

Lines changed: 4 additions & 3 deletions
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

Lines changed: 7 additions & 0 deletions
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)