Skip to content

Commit 4391db7

Browse files
authored
BUG: accepting ndarray[object] of dt64(nat) in TimedeltaIndex (#39462)
1 parent 99289e9 commit 4391db7

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Datetimelike
284284
Timedelta
285285
^^^^^^^^^
286286
- Bug in constructing :class:`Timedelta` from ``np.timedelta64`` objects with non-nanosecond units that are out of bounds for ``timedelta64[ns]`` (:issue:`38965`)
287-
-
287+
- Bug in constructing a :class:`TimedeltaIndex` incorrectly accepting ``np.datetime64("NaT")`` objects (:issue:`39462`)
288288
-
289289

290290
Timezones

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,6 @@ cdef convert_to_timedelta64(object ts, str unit):
257257
elif isinstance(ts, _Timedelta):
258258
# already in the proper format
259259
ts = np.timedelta64(ts.value, "ns")
260-
elif is_datetime64_object(ts):
261-
# only accept a NaT here
262-
if ts.astype("int64") == NPY_NAT:
263-
return np.timedelta64(NPY_NAT, "ns")
264260
elif is_timedelta64_object(ts):
265261
ts = ensure_td64ns(ts)
266262
elif is_integer_object(ts):

pandas/tests/frame/test_constructors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@
4848

4949

5050
class TestDataFrameConstructors:
51+
def test_array_of_dt64_nat_with_td64dtype_raises(self, frame_or_series):
52+
# GH#39462
53+
nat = np.datetime64("NaT", "ns")
54+
arr = np.array([nat], dtype=object)
55+
if frame_or_series is DataFrame:
56+
arr = arr.reshape(1, 1)
57+
58+
msg = "Could not convert object to NumPy timedelta"
59+
with pytest.raises(ValueError, match=msg):
60+
frame_or_series(arr, dtype="m8[ns]")
61+
5162
def test_series_with_name_not_matching_column(self):
5263
# GH#9232
5364
x = Series(range(5), name=1)

pandas/tests/indexes/timedeltas/test_constructors.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,26 @@
66
import pandas as pd
77
from pandas import Timedelta, TimedeltaIndex, timedelta_range, to_timedelta
88
import pandas._testing as tm
9-
from pandas.core.arrays import TimedeltaArray
9+
from pandas.core.arrays.timedeltas import TimedeltaArray, sequence_to_td64ns
1010

1111

1212
class TestTimedeltaIndex:
13+
def test_array_of_dt64_nat_raises(self):
14+
# GH#39462
15+
nat = np.datetime64("NaT", "ns")
16+
arr = np.array([nat], dtype=object)
17+
18+
# TODO: should be TypeError?
19+
msg = "Invalid type for timedelta scalar"
20+
with pytest.raises(ValueError, match=msg):
21+
TimedeltaIndex(arr)
22+
23+
with pytest.raises(ValueError, match=msg):
24+
TimedeltaArray._from_sequence(arr)
25+
26+
with pytest.raises(ValueError, match=msg):
27+
sequence_to_td64ns(arr)
28+
1329
@pytest.mark.parametrize("unit", ["Y", "y", "M"])
1430
def test_unit_m_y_raises(self, unit):
1531
msg = "Units 'M', 'Y', and 'y' are no longer supported"

0 commit comments

Comments
 (0)