Skip to content

TST: catch exception for div/truediv operations between Timedelta and pd.NA #58188

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
Show file tree
Hide file tree
Changes from 5 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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ Datetimelike
Timedelta
^^^^^^^^^
- Accuracy improvement in :meth:`Timedelta.to_pytimedelta` to round microseconds consistently for large nanosecond based Timedelta (:issue:`57841`)
- Add a test for div and true div between ``Timedelta`` and ``pd.NA`` raising a TypeError (:issue:`54315`)
Copy link
Member

Choose a reason for hiding this comment

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

Whatsnew notes are not needed for just unit test additions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I just removed it

-

Timezones
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pytest

from pandas._libs import lib
from pandas._libs.missing import NA
from pandas._libs.tslibs import (
NaT,
iNaT,
Expand Down Expand Up @@ -138,6 +139,19 @@ def test_truediv_numeric(self, td):
assert res._value == td._value / 2
assert res._creso == td._creso

def test_truediv_na_type_not_supported(self, td):
msg_td_floordiv_na = (
r"unsupported operand type\(s\) for /: 'Timedelta' and 'NAType'"
)
with pytest.raises(TypeError, match=msg_td_floordiv_na):
td / NA

msg_na_floordiv_td = (
r"unsupported operand type\(s\) for /: 'NAType' and 'Timedelta'"
)
with pytest.raises(TypeError, match=msg_na_floordiv_td):
NA / td

def test_floordiv_timedeltalike(self, td):
assert td // td == 1
assert (2.5 * td) // td == 2
Expand Down Expand Up @@ -182,6 +196,19 @@ def test_floordiv_numeric(self, td):
assert res._value == td._value // 2
assert res._creso == td._creso

def test_floordiv_na_type_not_supported(self, td):
msg_td_floordiv_na = (
r"unsupported operand type\(s\) for //: 'Timedelta' and 'NAType'"
)
with pytest.raises(TypeError, match=msg_td_floordiv_na):
td // NA

msg_na_floordiv_td = (
r"unsupported operand type\(s\) for //: 'NAType' and 'Timedelta'"
)
with pytest.raises(TypeError, match=msg_na_floordiv_td):
NA // td

def test_addsub_mismatched_reso(self, td):
# need to cast to since td is out of bounds for ns, so
# so we would raise OverflowError without casting
Expand Down