diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index 3d8e592c49221..e85ba505887b4 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -40,6 +40,7 @@ Backwards incompatible API changes Other API Changes ^^^^^^^^^^^^^^^^^ +- ``NaT`` division with :class:`datetime.timedelta` will now return ``NaN`` instead of raising (:issue:`17876`) - :class:`Timestamp` will no longer silently ignore unused or invalid `tz` or `tzinfo` arguments (:issue:`17690`) - - diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 0f39c2947dfa0..53cde4f9b6b65 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -1448,7 +1448,7 @@ _nat_scalar_rules[Py_GE] = False cdef _nat_divide_op(self, other): - if (isinstance(other, Timedelta) or + if (PyDelta_Check(other) or is_timedelta64_object(other) or other is NaT): return np.nan if is_integer_object(other) or is_float_object(other): @@ -1456,7 +1456,7 @@ cdef _nat_divide_op(self, other): return NotImplemented cdef _nat_rdivide_op(self, other): - if isinstance(other, Timedelta): + if PyDelta_Check(other): return np.nan return NotImplemented diff --git a/pandas/tests/scalar/test_nat.py b/pandas/tests/scalar/test_nat.py index 0e69371511294..7194849f19ebb 100644 --- a/pandas/tests/scalar/test_nat.py +++ b/pandas/tests/scalar/test_nat.py @@ -248,6 +248,8 @@ def test_nat_arithmetic(): assert left + right is NaT assert right - left is NaT assert left - right is NaT + assert np.isnan(left / right) + assert np.isnan(right / left) # GH 11718 t_utc = Timestamp('2014-01-01', tz='UTC')