diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 7a2175a364a8a..a46e74ae15a06 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1733,6 +1733,8 @@ def maybe_cast_to_datetime( except ValueError: # TODO(GH#40048): only catch dateutil's ParserError # once we can reliably import it in all supported versions + if is_timedelta64: + raise pass # coerce datetimelike to object diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index b76a44b3c86be..5edeb72725d2f 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -89,7 +89,12 @@ def test_array_of_dt64_nat_with_td64dtype_raises(self, frame_or_series): if frame_or_series is DataFrame: arr = arr.reshape(1, 1) - msg = "Could not convert object to NumPy timedelta" + msg = "|".join( + [ + "Could not convert object to NumPy timedelta", + "Invalid type for timedelta scalar: ", + ] + ) with pytest.raises(ValueError, match=msg): frame_or_series(arr, dtype="m8[ns]") diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index ab484e7ae9d8a..75474a29169a7 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1351,7 +1351,12 @@ def test_constructor_dtype_timedelta64(self): td.astype("int32") # this is an invalid casting - msg = "Could not convert object to NumPy timedelta" + msg = "|".join( + [ + "Could not convert object to NumPy timedelta", + "Could not convert 'foo' to NumPy timedelta", + ] + ) with pytest.raises(ValueError, match=msg): Series([timedelta(days=1), "foo"], dtype="m8[ns]")