Skip to content

Commit 4ba48f0

Browse files
authored
BUG: None / Timedelta incorrectly returning NaT (#32340)
1 parent 604beff commit 4ba48f0

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ Timedelta
214214
^^^^^^^^^
215215

216216
- Bug in constructing a :class:`Timedelta` with a high precision integer that would round the :class:`Timedelta` components (:issue:`31354`)
217+
- Bug in dividing ``np.nan`` or ``None`` by :class:`Timedelta`` incorrectly returning ``NaT`` (:issue:`31869`)
217218
-
218219

219220
Timezones

pandas/_libs/tslibs/timedeltas.pyx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1407,15 +1407,23 @@ class Timedelta(_Timedelta):
14071407
# convert to Timedelta below
14081408
pass
14091409

1410+
elif util.is_nan(other):
1411+
# i.e. np.nan or np.float64("NaN")
1412+
raise TypeError("Cannot divide float by Timedelta")
1413+
14101414
elif hasattr(other, 'dtype'):
1415+
if other.dtype.kind == "O":
1416+
# GH#31869
1417+
return np.array([x / self for x in other])
14111418
return other / self.to_timedelta64()
14121419

14131420
elif not _validate_ops_compat(other):
14141421
return NotImplemented
14151422

14161423
other = Timedelta(other)
14171424
if other is NaT:
1418-
return NaT
1425+
# In this context we treat NaT as timedelta-like
1426+
return np.nan
14191427
return float(other.value) / self.value
14201428

14211429
def __floordiv__(self, other):

pandas/tests/scalar/timedelta/test_arithmetic.py

+40
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,46 @@ def test_td_rdiv_timedeltalike_scalar(self):
412412

413413
assert np.timedelta64(60, "h") / td == 0.25
414414

415+
def test_td_rdiv_na_scalar(self):
416+
# GH#31869 None gets cast to NaT
417+
td = Timedelta(10, unit="d")
418+
419+
result = NaT / td
420+
assert np.isnan(result)
421+
422+
result = None / td
423+
assert np.isnan(result)
424+
425+
result = np.timedelta64("NaT") / td
426+
assert np.isnan(result)
427+
428+
with pytest.raises(TypeError, match="cannot use operands with types dtype"):
429+
np.datetime64("NaT") / td
430+
431+
with pytest.raises(TypeError, match="Cannot divide float by Timedelta"):
432+
np.nan / td
433+
434+
def test_td_rdiv_ndarray(self):
435+
td = Timedelta(10, unit="d")
436+
437+
arr = np.array([td], dtype=object)
438+
result = arr / td
439+
expected = np.array([1], dtype=np.float64)
440+
tm.assert_numpy_array_equal(result, expected)
441+
442+
arr = np.array([None])
443+
result = arr / td
444+
expected = np.array([np.nan])
445+
tm.assert_numpy_array_equal(result, expected)
446+
447+
arr = np.array([np.nan], dtype=object)
448+
with pytest.raises(TypeError, match="Cannot divide float by Timedelta"):
449+
arr / td
450+
451+
arr = np.array([np.nan], dtype=np.float64)
452+
with pytest.raises(TypeError, match="cannot use operands with types dtype"):
453+
arr / td
454+
415455
# ---------------------------------------------------------------
416456
# Timedelta.__floordiv__
417457

0 commit comments

Comments
 (0)