Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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