Skip to content

BUG: None / Timedelta incorrectly returning NaT #32340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Timedelta
^^^^^^^^^

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

Timezones
Expand Down
10 changes: 9 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1407,15 +1407,23 @@ class Timedelta(_Timedelta):
# convert to Timedelta below
pass

elif util.is_nan(other):
# i.e. np.nan or np.float64("NaN")
raise TypeError("Cannot divide float by Timedelta")

elif hasattr(other, 'dtype'):
if other.dtype.kind == "O":
# GH#31869
return np.array([x / self for x in other])
return other / self.to_timedelta64()

elif not _validate_ops_compat(other):
return NotImplemented

other = Timedelta(other)
if other is NaT:
return NaT
# In this context we treat NaT as timedelta-like
return np.nan
return float(other.value) / self.value

def __floordiv__(self, other):
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/scalar/timedelta/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,46 @@ def test_td_rdiv_timedeltalike_scalar(self):

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

def test_td_rdiv_na_scalar(self):
# GH#31869 None gets cast to NaT
td = Timedelta(10, unit="d")

result = NaT / td
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe as a follown split out the these non-error casees and use nulls_fixture (as you then can test float('nan')

assert np.isnan(result)

result = None / td
assert np.isnan(result)

result = np.timedelta64("NaT") / td
assert np.isnan(result)

with pytest.raises(TypeError, match="cannot use operands with types dtype"):
np.datetime64("NaT") / td

with pytest.raises(TypeError, match="Cannot divide float by Timedelta"):
np.nan / td

def test_td_rdiv_ndarray(self):
td = Timedelta(10, unit="d")

arr = np.array([td], dtype=object)
result = arr / td
expected = np.array([1], dtype=np.float64)
tm.assert_numpy_array_equal(result, expected)

arr = np.array([None])
result = arr / td
expected = np.array([np.nan])
tm.assert_numpy_array_equal(result, expected)

arr = np.array([np.nan], dtype=object)
with pytest.raises(TypeError, match="Cannot divide float by Timedelta"):
arr / td

arr = np.array([np.nan], dtype=np.float64)
with pytest.raises(TypeError, match="cannot use operands with types dtype"):
arr / td

# ---------------------------------------------------------------
# Timedelta.__floordiv__

Expand Down