Skip to content

BUG: fix timedelta floordiv with scalar float #44466

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ Datetimelike
Timedelta
^^^^^^^^^
- 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`)
-
- Bug in floor division of ``timedelta64[ns]`` data with a scalar returning garbage values (:issue:`44466`)

Timezones
^^^^^^^^^
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,7 @@ def __floordiv__(self, other):

# at this point we should only have numeric scalars; anything
# else will raise
result = self.asi8 // other
np.putmask(result, self._isnan, iNaT)
result = self._ndarray / other
Copy link
Member

Choose a reason for hiding this comment

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

why is this using div instead of floordiv?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because I was stupid .. :), and because the test I added (or any other test we already have) actually don't catch this because only use such data that it is the same for div or floordiv ..

Copy link
Member Author

Choose a reason for hiding this comment

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

-> #44471

freq = None
if self.freq is not None:
# Note: freq gets division, not floor-division
Expand All @@ -661,7 +660,7 @@ def __floordiv__(self, other):
# e.g. if self.freq is Nano(1) then dividing by 2
# rounds down to zero
freq = None
return type(self)(result.view("m8[ns]"), freq=freq)
return type(self)(result, freq=freq)

if not hasattr(other, "dtype"):
# list, tuple
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,20 @@ def test_td64arr_div_numeric_scalar(self, box_with_array, two):
with pytest.raises(TypeError, match="Cannot divide"):
two / tdser

@pytest.mark.parametrize("two", [2, 2.0, np.array(2), np.array(2.0)])
def test_td64arr_floordiv_numeric_scalar(self, box_with_array, two):
Copy link
Member Author

Choose a reason for hiding this comment

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

We have versions above of this test for multiplication and division, so adding one for floordiv as well

tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
expected = Series(["29.5D", "29.5D", "NaT"], dtype="timedelta64[ns]")

tdser = tm.box_expected(tdser, box_with_array)
expected = tm.box_expected(expected, box_with_array)

result = tdser // two
tm.assert_equal(result, expected)

with pytest.raises(TypeError, match="Cannot divide"):
two // tdser

@pytest.mark.parametrize(
"vector",
[np.array([20, 30, 40]), pd.Index([20, 30, 40]), Series([20, 30, 40])],
Expand Down