Skip to content

Commit 5cdde87

Browse files
BUG: fix timedelta floordiv with scalar float (#44466)
1 parent a21eae0 commit 5cdde87

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ Datetimelike
545545
Timedelta
546546
^^^^^^^^^
547547
- Bug in division of all-``NaT`` :class:`TimeDeltaIndex`, :class:`Series` or :class:`DataFrame` column with object-dtype arraylike of numbers failing to infer the result as timedelta64-dtype (:issue:`39750`)
548-
-
548+
- Bug in floor division of ``timedelta64[ns]`` data with a scalar returning garbage values (:issue:`44466`)
549549

550550
Timezones
551551
^^^^^^^^^

pandas/core/arrays/timedeltas.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,7 @@ def __floordiv__(self, other):
651651

652652
# at this point we should only have numeric scalars; anything
653653
# else will raise
654-
result = self.asi8 // other
655-
np.putmask(result, self._isnan, iNaT)
654+
result = self._ndarray / other
656655
freq = None
657656
if self.freq is not None:
658657
# Note: freq gets division, not floor-division
@@ -661,7 +660,7 @@ def __floordiv__(self, other):
661660
# e.g. if self.freq is Nano(1) then dividing by 2
662661
# rounds down to zero
663662
freq = None
664-
return type(self)(result.view("m8[ns]"), freq=freq)
663+
return type(self)(result, freq=freq)
665664

666665
if not hasattr(other, "dtype"):
667666
# list, tuple

pandas/tests/arithmetic/test_timedelta64.py

+14
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,20 @@ def test_td64arr_div_numeric_scalar(self, box_with_array, two):
19811981
with pytest.raises(TypeError, match="Cannot divide"):
19821982
two / tdser
19831983

1984+
@pytest.mark.parametrize("two", [2, 2.0, np.array(2), np.array(2.0)])
1985+
def test_td64arr_floordiv_numeric_scalar(self, box_with_array, two):
1986+
tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
1987+
expected = Series(["29.5D", "29.5D", "NaT"], dtype="timedelta64[ns]")
1988+
1989+
tdser = tm.box_expected(tdser, box_with_array)
1990+
expected = tm.box_expected(expected, box_with_array)
1991+
1992+
result = tdser // two
1993+
tm.assert_equal(result, expected)
1994+
1995+
with pytest.raises(TypeError, match="Cannot divide"):
1996+
two // tdser
1997+
19841998
@pytest.mark.parametrize(
19851999
"vector",
19862000
[np.array([20, 30, 40]), pd.Index([20, 30, 40]), Series([20, 30, 40])],

0 commit comments

Comments
 (0)