Skip to content

Commit b81f433

Browse files
author
Daniel Saxton
committed
Handle timedelta64
1 parent 9419744 commit b81f433

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,8 @@ def astype_nansafe(arr, dtype, copy=True, skipna=False):
713713
if is_object_dtype(dtype):
714714
return tslibs.ints_to_pytimedelta(arr.view(np.int64))
715715
elif dtype == np.int64:
716+
if isna(arr).any():
717+
raise ValueError("Cannot convert NaT values to integer")
716718
return arr.view(dtype)
717719

718720
if dtype not in [_INT64_DTYPE, _TD_DTYPE]:

pandas/tests/dtypes/test_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,8 @@ def test__is_dtype_type(input_param, result):
709709
assert com._is_dtype_type(input_param, lambda tipo: tipo == result)
710710

711711

712-
@pytest.mark.parametrize("val", [np.datetime64("NaT")])
713-
@pytest.mark.parametrize("typ", [np.int8, np.int16, np.int32, np.int64])
712+
@pytest.mark.parametrize("val", [np.datetime64("NaT"), np.timedelta64("NaT")])
713+
@pytest.mark.parametrize("typ", [np.int64])
714714
def test_astype_nansafe(val, typ):
715715
arr = np.array([val])
716716

0 commit comments

Comments
 (0)