From 94081e3a9299c721fc1ad575e7d6ea5ddce25889 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 28 Jan 2021 17:22:01 -0800 Subject: [PATCH 1/2] BUG: accepting ndarray[object] of dt64(nat) in TimedeltaIndex --- pandas/_libs/tslibs/timedeltas.pyx | 4 ---- .../indexes/timedeltas/test_constructors.py | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index f3bf45f681b1f..2def5f878e8e1 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -257,10 +257,6 @@ cdef convert_to_timedelta64(object ts, str unit): elif isinstance(ts, _Timedelta): # already in the proper format ts = np.timedelta64(ts.value, "ns") - elif is_datetime64_object(ts): - # only accept a NaT here - if ts.astype('int64') == NPY_NAT: - return np.timedelta64(NPY_NAT) elif is_timedelta64_object(ts): ts = ensure_td64ns(ts) elif is_integer_object(ts): diff --git a/pandas/tests/indexes/timedeltas/test_constructors.py b/pandas/tests/indexes/timedeltas/test_constructors.py index a07977702531e..5c9ba25cf18df 100644 --- a/pandas/tests/indexes/timedeltas/test_constructors.py +++ b/pandas/tests/indexes/timedeltas/test_constructors.py @@ -6,10 +6,25 @@ import pandas as pd from pandas import Timedelta, TimedeltaIndex, timedelta_range, to_timedelta import pandas._testing as tm -from pandas.core.arrays import TimedeltaArray +from pandas.core.arrays.timedeltas import TimedeltaArray, sequence_to_td64ns class TestTimedeltaIndex: + def test_array_of_dt64_nat_raises(self): + nat = np.datetime64("NaT", "ns") + arr = np.array([nat], dtype=object) + + # TODO: should be TypeError? + msg = "Invalid type for timedelta scalar" + with pytest.raises(ValueError, match=msg): + TimedeltaIndex(arr) + + with pytest.raises(ValueError, match=msg): + TimedeltaArray._from_sequence(arr) + + with pytest.raises(ValueError, match=msg): + sequence_to_td64ns(arr) + @pytest.mark.parametrize("unit", ["Y", "y", "M"]) def test_unit_m_y_raises(self, unit): msg = "Units 'M', 'Y', and 'y' are no longer supported" From 240c6b9d333e075bc7316da98be1747f777bd493 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 28 Jan 2021 19:12:01 -0800 Subject: [PATCH 2/2] whatsnew, tests for Series and DataFrame --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/tests/frame/test_constructors.py | 11 +++++++++++ pandas/tests/indexes/timedeltas/test_constructors.py | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 7fe0b53d7d2ff..3aa41e1d53057 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -284,7 +284,7 @@ Datetimelike Timedelta ^^^^^^^^^ - Bug in constructing :class:`Timedelta` from ``np.timedelta64`` objects with non-nanosecond units that are out of bounds for ``timedelta64[ns]`` (:issue:`38965`) -- +- Bug in constructing a :class:`TimedeltaIndex` incorrectly accepting ``np.datetime64("NaT")`` objects (:issue:`39462`) - Timezones diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index f23b8d559a00a..9ec745932514f 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -48,6 +48,17 @@ class TestDataFrameConstructors: + def test_array_of_dt64_nat_with_td64dtype_raises(self, frame_or_series): + # GH#39462 + nat = np.datetime64("NaT", "ns") + arr = np.array([nat], dtype=object) + if frame_or_series is DataFrame: + arr = arr.reshape(1, 1) + + msg = "Could not convert object to NumPy timedelta" + with pytest.raises(ValueError, match=msg): + frame_or_series(arr, dtype="m8[ns]") + def test_series_with_name_not_matching_column(self): # GH#9232 x = Series(range(5), name=1) diff --git a/pandas/tests/indexes/timedeltas/test_constructors.py b/pandas/tests/indexes/timedeltas/test_constructors.py index 5c9ba25cf18df..ffc10faaf8150 100644 --- a/pandas/tests/indexes/timedeltas/test_constructors.py +++ b/pandas/tests/indexes/timedeltas/test_constructors.py @@ -11,6 +11,7 @@ class TestTimedeltaIndex: def test_array_of_dt64_nat_raises(self): + # GH#39462 nat = np.datetime64("NaT", "ns") arr = np.array([nat], dtype=object)