Skip to content

Commit c660e2a

Browse files
jbrockmendelharisbal
authored and
harisbal
committed
fix Timedelta.__mul__(NaT) (pandas-dev#19819)
1 parent f3836c4 commit c660e2a

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ Datetimelike
757757
Timedelta
758758
^^^^^^^^^
759759

760+
- Bug in :func:`Timedelta.__mul__` where multiplying by ``NaT`` returned ``NaT`` instead of raising a ``TypeError`` (:issue:`19819`)
760761
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`)
761762
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (:issue:`19043`)
762763
- Bug in :func:`Timedelta.__floordiv__` and :func:`Timedelta.__rfloordiv__` dividing by many incompatible numpy objects was incorrectly allowed (:issue:`18846`)

pandas/_libs/tslibs/timedeltas.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ class Timedelta(_Timedelta):
10571057
return other * self.to_timedelta64()
10581058

10591059
elif other is NaT:
1060-
return NaT
1060+
raise TypeError('Cannot multiply Timedelta with NaT')
10611061

10621062
elif not (is_integer_object(other) or is_float_object(other)):
10631063
# only integers and floats allowed

pandas/tests/scalar/timedelta/test_arithmetic.py

+10
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ class TestTimedeltaMultiplicationDivision(object):
216216
# ---------------------------------------------------------------
217217
# Timedelta.__mul__, __rmul__
218218

219+
@pytest.mark.parametrize('td_nat', [pd.NaT,
220+
np.timedelta64('NaT', 'ns'),
221+
np.timedelta64('NaT')])
222+
@pytest.mark.parametrize('op', [operator.mul, ops.rmul])
223+
def test_td_mul_nat(self, op, td_nat):
224+
# GH#19819
225+
td = Timedelta(10, unit='d')
226+
with pytest.raises(TypeError):
227+
op(td, td_nat)
228+
219229
@pytest.mark.parametrize('op', [operator.mul, ops.rmul])
220230
def test_td_mul_scalar(self, op):
221231
# GH#19738

pandas/tests/scalar/timedelta/test_timedelta.py

-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ def test_unary_ops(self):
6262
assert abs(-td) == td
6363
assert abs(-td) == Timedelta('10d')
6464

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

7166
class TestTimedeltaComparison(object):
7267
def test_comparison_object_array(self):

0 commit comments

Comments
 (0)