Skip to content

Commit

Permalink
BUG: Don't ignore errors when casting dtype in Series constructor (pa…
Browse files Browse the repository at this point in the history
…ndas-dev#60882)

* BUG: Don't ignore errors when casting dtype in Series constructor

* Add test and whatsnew
  • Loading branch information
snitish authored Feb 8, 2025
1 parent bfbf991 commit e557039
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ Styler
Other
^^^^^
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
- Bug in :class:`Series` ignoring errors when trying to convert :class:`Series` input data to the given ``dtype`` (:issue:`60728`)
- Bug in :func:`eval` on :class:`ExtensionArray` on including division ``/`` failed with a ``TypeError``. (:issue:`58748`)
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
- Bug in :func:`eval` with ``engine="numexpr"`` returning unexpected result for float division. (:issue:`59736`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def __init__(
# create/copy the manager
if isinstance(data, SingleBlockManager):
if dtype is not None:
data = data.astype(dtype=dtype, errors="ignore")
data = data.astype(dtype=dtype)
elif copy:
data = data.copy()
else:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def test_unparsable_strings_with_dt64_dtype(self):
with pytest.raises(ValueError, match=msg):
Series(np.array(vals, dtype=object), dtype="datetime64[ns]")

def test_invalid_dtype_conversion_datetime_to_timedelta(self):
# GH#60728
vals = Series([NaT, Timestamp(2025, 1, 1)], dtype="datetime64[ns]")
msg = r"^Cannot cast DatetimeArray to dtype timedelta64\[ns\]$"
with pytest.raises(TypeError, match=msg):
Series(vals, dtype="timedelta64[ns]")

@pytest.mark.parametrize(
"constructor",
[
Expand Down

0 comments on commit e557039

Please sign in to comment.