Skip to content

Commit bdcefdd

Browse files
authored
BUG: maybe_convert_objects with timedelta64 nat (#40062)
1 parent 6efc5af commit bdcefdd

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
22502250
break
22512251
elif is_timedelta(val):
22522252
if convert_timedelta:
2253-
itimedeltas[i] = convert_to_timedelta64(val, 'ns')
2253+
itimedeltas[i] = convert_to_timedelta64(val, "ns").view("i8")
22542254
seen.timedelta_ = True
22552255
else:
22562256
seen.object_ = True

pandas/tests/dtypes/test_inference.py

+10
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,16 @@ def test_maybe_convert_objects_datetime(self):
583583
)
584584
tm.assert_numpy_array_equal(out, exp)
585585

586+
def test_maybe_convert_objects_timedelta64_nat(self):
587+
obj = np.timedelta64("NaT", "ns")
588+
arr = np.array([obj], dtype=object)
589+
assert arr[0] is obj
590+
591+
result = lib.maybe_convert_objects(arr, convert_timedelta=True)
592+
593+
expected = np.array([obj], dtype="m8[ns]")
594+
tm.assert_numpy_array_equal(result, expected)
595+
586596
@pytest.mark.parametrize(
587597
"exp",
588598
[

0 commit comments

Comments
 (0)