Skip to content

BUG: construction from dt64/td64 values with td64/dt64 dtype #38575

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 3 commits into from
Dec 23, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Datetimelike
^^^^^^^^^^^^
- Bug in :class:`DataFrame` and :class:`Series` constructors sometimes dropping nanoseconds from :class:`Timestamp` (resp. :class:`Timedelta`) ``data``, with ``dtype=datetime64[ns]`` (resp. ``timedelta64[ns]``) (:issue:`38032`)
- Bug in :meth:`DataFrame.first` and :meth:`Series.first` returning two months for offset one month when first day is last calendar day (:issue:`29623`)
-
- Bug in constructing a :class:`DataFrame` or :class:`Series` with mismatched ``datetime64`` data and ``timedelta64`` dtype, or vice-versa, failing to raise ``TypeError`` (:issue:`38575`)

Timedelta
^^^^^^^^^
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ def maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar:
value = value.to_datetime64()
elif isinstance(value, Timedelta):
value = value.to_timedelta64()

if (isinstance(value, np.timedelta64) and dtype.kind == "M") or (
isinstance(value, np.datetime64) and dtype.kind == "m"
):
# numpy allows np.array(dt64values, dtype="timedelta64[ns]") and
# vice-versa, but we do not want to allow this, so we need to
# check explicitly
raise TypeError(f"Cannot cast {repr(value)} to {dtype}")
return value


Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/dtypes/cast/test_construct_from_scalar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

from pandas.core.dtypes.cast import construct_1d_arraylike_from_scalar
from pandas.core.dtypes.dtypes import CategoricalDtype
Expand Down Expand Up @@ -32,3 +33,20 @@ def test_cast_1d_array_like_from_timedelta():
td = Timedelta(1)
res = construct_1d_arraylike_from_scalar(td, 2, np.dtype("m8[ns]"))
assert res[0] == td


def test_cast_1d_array_like_mismatched_datetimelike():
td = np.timedelta64("NaT", "ns")
dt = np.datetime64("NaT", "ns")

with pytest.raises(TypeError, match="Cannot cast"):
construct_1d_arraylike_from_scalar(td, 2, dt.dtype)

with pytest.raises(TypeError, match="Cannot cast"):
construct_1d_arraylike_from_scalar(np.timedelta64(4, "ns"), 2, dt.dtype)

with pytest.raises(TypeError, match="Cannot cast"):
construct_1d_arraylike_from_scalar(dt, 2, td.dtype)

with pytest.raises(TypeError, match="Cannot cast"):
construct_1d_arraylike_from_scalar(np.datetime64(4, "ns"), 2, td.dtype)
12 changes: 12 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2962,3 +2962,15 @@ def test_from_timedelta64_scalar_object(self, constructor, request):

obj = constructor(td64, dtype=object)
assert isinstance(get1(obj), np.timedelta64)

@pytest.mark.parametrize("cls", [np.datetime64, np.timedelta64])
def test_from_scalar_datetimelike_mismatched(self, constructor, cls):
scalar = cls("NaT", "ns")
dtype = {np.datetime64: "m8[ns]", np.timedelta64: "M8[ns]"}[cls]

with pytest.raises(TypeError, match="Cannot cast"):
constructor(scalar, dtype=dtype)

scalar = cls(4, "ns")
with pytest.raises(TypeError, match="Cannot cast"):
constructor(scalar, dtype=dtype)