Skip to content

Commit 278c69b

Browse files
authored
API: stop silently ignoring parsing failures with dtype=dt64 (#49358)
1 parent 321157d commit 278c69b

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Other API changes
142142
- The ``other`` argument in :meth:`DataFrame.mask` and :meth:`Series.mask` now defaults to ``no_default`` instead of ``np.nan`` consistent with :meth:`DataFrame.where` and :meth:`Series.where`. Entries will be filled with the corresponding NULL value (``np.nan`` for numpy dtypes, ``pd.NA`` for extension dtypes). (:issue:`49111`)
143143
- When creating a :class:`Series` with a object-dtype :class:`Index` of datetime objects, pandas no longer silently converts the index to a :class:`DatetimeIndex` (:issue:`39307`, :issue:`23598`)
144144
- :meth:`Series.unique` with dtype "timedelta64[ns]" or "datetime64[ns]" now returns :class:`TimedeltaArray` or :class:`DatetimeArray` instead of ``numpy.ndarray`` (:issue:`49176`)
145+
- Passing strings that cannot be parsed as datetimes to :class:`Series` or :class:`DataFrame` with ``dtype="datetime64[ns]"`` will raise instead of silently ignoring the keyword and returning ``object`` dtype (:issue:`24435`)
145146
-
146147

147148
.. ---------------------------------------------------------------------------

pandas/core/dtypes/cast.py

+15-23
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
)
2121
import warnings
2222

23-
from dateutil.parser import ParserError
2423
import numpy as np
2524

2625
from pandas._libs import lib
@@ -1339,28 +1338,21 @@ def maybe_cast_to_datetime(
13391338
if value.size or not is_dtype_equal(value.dtype, dtype):
13401339
_disallow_mismatched_datetimelike(value, dtype)
13411340

1342-
try:
1343-
dta = sequence_to_datetimes(value)
1344-
# GH 25843: Remove tz information since the dtype
1345-
# didn't specify one
1346-
1347-
if dta.tz is not None:
1348-
raise ValueError(
1349-
"Cannot convert timezone-aware data to "
1350-
"timezone-naive dtype. Use "
1351-
"pd.Series(values).dt.tz_localize(None) instead."
1352-
)
1353-
1354-
# TODO(2.0): Do this astype in sequence_to_datetimes to
1355-
# avoid potential extra copy?
1356-
dta = dta.astype(dtype, copy=False)
1357-
value = dta
1358-
1359-
except OutOfBoundsDatetime:
1360-
raise
1361-
except ParserError:
1362-
# Note: this is dateutil's ParserError, not ours.
1363-
pass
1341+
dta = sequence_to_datetimes(value)
1342+
# GH 25843: Remove tz information since the dtype
1343+
# didn't specify one
1344+
1345+
if dta.tz is not None:
1346+
raise ValueError(
1347+
"Cannot convert timezone-aware data to "
1348+
"timezone-naive dtype. Use "
1349+
"pd.Series(values).dt.tz_localize(None) instead."
1350+
)
1351+
1352+
# TODO(2.0): Do this astype in sequence_to_datetimes to
1353+
# avoid potential extra copy?
1354+
dta = dta.astype(dtype, copy=False)
1355+
value = dta
13641356

13651357
elif getattr(vdtype, "kind", None) in ["m", "M"]:
13661358
# we are already datetimelike and want to coerce to non-datetimelike;

pandas/tests/frame/test_block_internals.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
DataFrame,
1717
Series,
1818
Timestamp,
19-
compat,
2019
date_range,
2120
option_context,
2221
)
@@ -266,8 +265,8 @@ def f(dtype):
266265
f("float64")
267266

268267
# 10822
269-
# invalid error message on dt inference
270-
if not compat.is_platform_windows():
268+
msg = "Unknown string format: aa present at position 0"
269+
with pytest.raises(ValueError, match=msg):
271270
f("M8[ns]")
272271

273272
def test_pickle(self, float_string_frame, timezone_frame):

pandas/tests/series/test_constructors.py

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252

5353

5454
class TestSeriesConstructors:
55+
def test_unparseable_strings_with_dt64_dtype(self):
56+
# pre-2.0 these would be silently ignored and come back with object dtype
57+
vals = ["aa"]
58+
msg = "Unknown string format: aa present at position 0"
59+
with pytest.raises(ValueError, match=msg):
60+
Series(vals, dtype="datetime64[ns]")
61+
62+
with pytest.raises(ValueError, match=msg):
63+
Series(np.array(vals, dtype=object), dtype="datetime64[ns]")
64+
5565
@pytest.mark.parametrize(
5666
"constructor,check_index_type",
5767
[

0 commit comments

Comments
 (0)