Skip to content

BUG: Timedelta.__new__ #48898

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 5 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ Datetimelike
Timedelta
^^^^^^^^^
- Bug in :func:`to_timedelta` raising error when input has nullable dtype ``Float64`` (:issue:`48796`)
- Bug in :class:`Timedelta` constructor incorrectly raising instead of returning ``NaT`` when given a ``np.timedelta64("nat")`` (:issue:`48898`)
- Bug in :class:`Timedelta` constructor failing to raise when passed both a :class:`Timedelta` object and keywords (e.g. days, seconds) (:issue:`48898`)
-

Timezones
Expand Down
14 changes: 11 additions & 3 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1662,10 +1662,15 @@ class Timedelta(_Timedelta):

# GH 30543 if pd.Timedelta already passed, return it
# check that only value is passed
if isinstance(value, _Timedelta) and unit is None and len(kwargs) == 0:
if isinstance(value, _Timedelta):
# 'unit' is benign in this case, but e.g. days or seconds
# doesn't make sense here.
if len(kwargs):
# GH#48898
raise ValueError(
"Cannot pass both a Timedelta input and timedelta keyword arguments."
Copy link
Member

Choose a reason for hiding this comment

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

Might be nice to show what kwags were passed

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea, updated

Copy link
Member Author

Choose a reason for hiding this comment

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

updated+green

)
return value
elif isinstance(value, _Timedelta):
value = value.value
elif isinstance(value, str):
if unit is not None:
raise ValueError("unit must not be specified if the value is a str")
Expand All @@ -1679,6 +1684,9 @@ class Timedelta(_Timedelta):
elif PyDelta_Check(value):
value = convert_to_timedelta64(value, 'ns')
elif is_timedelta64_object(value):
if get_timedelta64_value(value) == NPY_NAT:
# i.e. np.timedelta64("NaT")
return NaT
value = ensure_td64ns(value)
elif is_tick_object(value):
value = np.timedelta64(value.nanos, 'ns')
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/scalar/timedelta/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pandas._libs.tslibs import OutOfBoundsTimedelta

from pandas import (
NaT,
Timedelta,
offsets,
to_timedelta,
Expand Down Expand Up @@ -371,6 +372,14 @@ def test_timedelta_constructor_identity():
assert result is expected


def test_timedelta_pass_td_and_kwargs_raises():
# don't silently ignore the kwargs GH#48898
td = Timedelta(days=1)
msg = "Cannot pass both a Timedelta input and timedelta keyword arguments"
with pytest.raises(ValueError, match=msg):
Timedelta(td, days=2)


@pytest.mark.parametrize(
"constructor, value, unit, expectation",
[
Expand Down Expand Up @@ -402,3 +411,9 @@ def test_string_without_numbers(value):
)
with pytest.raises(ValueError, match=msg):
Timedelta(value)


def test_timedelta_new_npnat():
# GH#48898
nat = np.timedelta64("NaT", "h")
assert Timedelta(nat) is NaT