Skip to content

Conversation

snitish
Copy link
Member

@snitish snitish commented Feb 8, 2025

In the constructor for Series, when the input data is another Series with a different dtype than the one specified in the constructor, the dtype conversion currently doesn't throw errors and silently sets the wrong dtype.

>>> example = pd.Series(
...     pd.Series([pd.NaT], dtype="datetime64[ns]"), dtype="timedelta64[ns]"
... )
>>> print(example.dtype)
datetime64[ns]

@snitish snitish marked this pull request as draft February 8, 2025 03:02
@mroeschke mroeschke added this to the 3.0 milestone Feb 8, 2025
@mroeschke mroeschke added the Dtype Conversions Unexpected or buggy dtype conversions label Feb 8, 2025
@mroeschke mroeschke merged commit e557039 into pandas-dev:main Feb 8, 2025
45 checks passed
@mroeschke
Copy link
Member

Thanks again @snitish

@rhshadrach
Copy link
Member

With this PR, this now raises:

pd.Series([1.5], dtype="int64")

While perhaps we want to do this in the future, I think it can have a large impact and we definitely should not do it without proper deprecation. Ref: #60728 (comment)

@rhshadrach
Copy link
Member

Ah, ignore me, that line always raised. I confused it with

pd.Series(pd.Series([1.5], dtype="float64"), dtype="int64")

which still gives int64 dtype as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Dtype Conversions Unexpected or buggy dtype conversions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: pd.Series fails to cast datetime series containing only NaT to timedelta type

3 participants