Skip to content

fix Timedelta.__mul__(NaT) #19819

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
Feb 22, 2018
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/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ Datetimelike
Timedelta
^^^^^^^^^

- Bug in :func:`Timedelta.__mul__` where multiplying by ``NaT`` returned ``NaT`` instead of raising a ``TypeError`` (:issue:`19819`)
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`)
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (:issue:`19043`)
- Bug in :func:`Timedelta.__floordiv__` and :func:`Timedelta.__rfloordiv__` dividing by many incompatible numpy objects was incorrectly allowed (:issue:`18846`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ class Timedelta(_Timedelta):
return other * self.to_timedelta64()

elif other is NaT:
return NaT
raise TypeError('Cannot multiply Timedelta with NaT')

elif not (is_integer_object(other) or is_float_object(other)):
# only integers and floats allowed
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/scalar/timedelta/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ class TestTimedeltaMultiplicationDivision(object):
# ---------------------------------------------------------------
# Timedelta.__mul__, __rmul__

@pytest.mark.parametrize('td_nat', [pd.NaT,
np.timedelta64('NaT', 'ns'),
np.timedelta64('NaT')])
@pytest.mark.parametrize('op', [operator.mul, ops.rmul])
def test_td_mul_nat(self, op, td_nat):
# GH#19819
td = Timedelta(10, unit='d')
with pytest.raises(TypeError):
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we raise on this again? what is the behavior on divide?

Copy link
Member Author

Choose a reason for hiding this comment

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

For the same reason we raise on Timedelta * timedelta (or Timedelta * datetime depending on what hat pd.NaT is wearing).

For division Timedelta / pd.NaT returns np.nan, which is already correct.

op(td, td_nat)

@pytest.mark.parametrize('op', [operator.mul, ops.rmul])
def test_td_mul_scalar(self, op):
# GH#19738
Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ def test_unary_ops(self):
assert abs(-td) == td
assert abs(-td) == Timedelta('10d')

def test_binary_ops_nat(self):
td = Timedelta(10, unit='d')
# FIXME: The next test is wrong: td * NaT should raise
assert (td * pd.NaT) is pd.NaT


class TestTimedeltaComparison(object):
def test_comparison_object_array(self):
Expand Down