Skip to content

Commit ca27ee9

Browse files
jbrockmendeljreback
authored andcommitted
Fix rfloordiv return type, un-xfail Timedelta tests (#19820)
1 parent 399a96b commit ca27ee9

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ Timedelta
767767
- Bug in :func:`Timedelta.__floordiv__`, :func:`Timedelta.__rfloordiv__` where operating with a ``Tick`` object would raise a ``TypeError`` instead of returning a numeric value (:issue:`19738`)
768768
- Bug in :func:`Period.asfreq` where periods near ``datetime(1, 1, 1)`` could be converted incorrectly (:issue:`19643`)
769769
- Bug in :func:`Timedelta.total_seconds()` causing precision errors i.e. ``Timedelta('30S').total_seconds()==30.000000000000004`` (:issue:`19458`)
770+
- Bug in :func: `Timedelta.__rmod__` where operating with a ``numpy.timedelta64`` returned a ``timedelta64`` object instead of a ``Timedelta`` (:issue:`19820`)
770771
- Multiplication of :class:`TimedeltaIndex` by ``TimedeltaIndex`` will now raise ``TypeError`` instead of raising ``ValueError`` in cases of length mis-match (:issue`19333`)
771772
-
772773

pandas/_libs/tslibs/timedeltas.pyx

+10-2
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,11 @@ class Timedelta(_Timedelta):
11091109
return self // other.delta
11101110
return NotImplemented
11111111

1112-
if hasattr(other, 'dtype'):
1112+
elif is_timedelta64_object(other):
1113+
# convert to Timedelta below
1114+
pass
1115+
1116+
elif hasattr(other, 'dtype'):
11131117
if other.dtype.kind == 'm':
11141118
# also timedelta-like
11151119
return _broadcast_floordiv_td64(self.value, other, _floordiv)
@@ -1144,7 +1148,11 @@ class Timedelta(_Timedelta):
11441148
return other.delta // self
11451149
return NotImplemented
11461150

1147-
if hasattr(other, 'dtype'):
1151+
elif is_timedelta64_object(other):
1152+
# convert to Timedelta below
1153+
pass
1154+
1155+
elif hasattr(other, 'dtype'):
11481156
if other.dtype.kind == 'm':
11491157
# also timedelta-like
11501158
return _broadcast_floordiv_td64(self.value, other, _rfloordiv)

pandas/tests/scalar/timedelta/test_arithmetic.py

-6
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,13 @@ def test_mod_timedeltalike(self):
451451
result = td % NaT
452452
assert result is NaT
453453

454-
@pytest.mark.xfail(reason='GH#19378 floordiv td64 returns td64')
455454
def test_mod_timedelta64_nat(self):
456455
# GH#19365
457456
td = Timedelta(hours=37)
458457

459458
result = td % np.timedelta64('NaT', 'ns')
460459
assert result is NaT
461460

462-
@pytest.mark.xfail(reason='GH#19378 floordiv td64 returns td64')
463461
def test_mod_timedelta64(self):
464462
# GH#19365
465463
td = Timedelta(hours=37)
@@ -468,7 +466,6 @@ def test_mod_timedelta64(self):
468466
assert isinstance(result, Timedelta)
469467
assert result == Timedelta(hours=1)
470468

471-
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented')
472469
def test_mod_offset(self):
473470
# GH#19365
474471
td = Timedelta(hours=37)
@@ -515,7 +512,6 @@ def test_rmod_pytimedelta(self):
515512
assert isinstance(result, Timedelta)
516513
assert result == Timedelta(minutes=1)
517514

518-
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented')
519515
def test_rmod_timedelta64(self):
520516
# GH#19365
521517
td = Timedelta(minutes=3)
@@ -574,7 +570,6 @@ def test_divmod(self):
574570
assert np.isnan(result[0])
575571
assert result[1] is pd.NaT
576572

577-
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented')
578573
def test_divmod_offset(self):
579574
# GH#19365
580575
td = Timedelta(days=2, hours=6)
@@ -598,7 +593,6 @@ def test_rdivmod_pytimedelta(self):
598593
assert isinstance(result[1], Timedelta)
599594
assert result[1] == Timedelta(hours=6)
600595

601-
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented')
602596
def test_rdivmod_offset(self):
603597
result = divmod(pd.offsets.Hour(54), Timedelta(hours=-4))
604598
assert result[0] == -14

0 commit comments

Comments
 (0)