Skip to content

Commit c64f564

Browse files
miggecjreback
authored andcommitted
BUG: Fix to_datetime(errors='coerce') not swallowing all parser exceptions... (#28367)
1 parent 094fb12 commit c64f564

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Diff for: doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ Datetimelike
115115
- Bug in :meth:`Series.__setitem__` incorrectly casting ``np.timedelta64("NaT")`` to ``np.datetime64("NaT")`` when inserting into a :class:`Series` with datetime64 dtype (:issue:`27311`)
116116
- Bug in :meth:`Series.dt` property lookups when the underlying data is read-only (:issue:`27529`)
117117
- Bug in ``HDFStore.__getitem__`` incorrectly reading tz attribute created in Python 2 (:issue:`26443`)
118+
- Bug in :func:`to_datetime` where passing arrays of malformed ``str`` with errors="coerce" could incorrectly lead to raising ``ValueError`` (:issue:`28299`)
118119
- Bug in :meth:`pandas.core.groupby.SeriesGroupBy.nunique` where ``NaT`` values were interfering with the count of unique values (:issue:`27951`)
119120
- Bug in :class:`Timestamp` subtraction when subtracting a :class:`Timestamp` from a ``np.datetime64`` object incorrectly raising ``TypeError`` (:issue:`28286`)
120121
- Addition and subtraction of integer or integer-dtype arrays with :class:`Timestamp` will now raise ``NullFrequencyError`` instead of ``ValueError`` (:issue:`28268`)
121-
-
122122

123123

124124
Timedelta

Diff for: pandas/_libs/tslib.pyx

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

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

Diff for: 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)