Skip to content

Commit e557039

Browse files
authored
BUG: Don't ignore errors when casting dtype in Series constructor (pandas-dev#60882)
* BUG: Don't ignore errors when casting dtype in Series constructor * Add test and whatsnew
1 parent bfbf991 commit e557039

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ Styler
793793
Other
794794
^^^^^
795795
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
796+
- Bug in :class:`Series` ignoring errors when trying to convert :class:`Series` input data to the given ``dtype`` (:issue:`60728`)
796797
- Bug in :func:`eval` on :class:`ExtensionArray` on including division ``/`` failed with a ``TypeError``. (:issue:`58748`)
797798
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
798799
- Bug in :func:`eval` with ``engine="numexpr"`` returning unexpected result for float division. (:issue:`59736`)

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def __init__(
500500
# create/copy the manager
501501
if isinstance(data, SingleBlockManager):
502502
if dtype is not None:
503-
data = data.astype(dtype=dtype, errors="ignore")
503+
data = data.astype(dtype=dtype)
504504
elif copy:
505505
data = data.copy()
506506
else:

pandas/tests/series/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ def test_unparsable_strings_with_dt64_dtype(self):
9090
with pytest.raises(ValueError, match=msg):
9191
Series(np.array(vals, dtype=object), dtype="datetime64[ns]")
9292

93+
def test_invalid_dtype_conversion_datetime_to_timedelta(self):
94+
# GH#60728
95+
vals = Series([NaT, Timestamp(2025, 1, 1)], dtype="datetime64[ns]")
96+
msg = r"^Cannot cast DatetimeArray to dtype timedelta64\[ns\]$"
97+
with pytest.raises(TypeError, match=msg):
98+
Series(vals, dtype="timedelta64[ns]")
99+
93100
@pytest.mark.parametrize(
94101
"constructor",
95102
[

0 commit comments

Comments
 (0)