Skip to content

BUG: Timedelta with invalid keyword #61883

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 1 commit into from
Jul 16, 2025
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
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 @@ -717,6 +717,7 @@ Datetimelike
Timedelta
^^^^^^^^^
- Accuracy improvement in :meth:`Timedelta.to_pytimedelta` to round microseconds consistently for large nanosecond based Timedelta (:issue:`57841`)
- Bug in :class:`Timedelta` constructor failing to raise when passed an invalid keyword (:issue:`53801`)
- Bug in :meth:`DataFrame.cumsum` which was raising ``IndexError`` if dtype is ``timedelta64[ns]`` (:issue:`57956`)

Timezones
Expand Down
24 changes: 14 additions & 10 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,20 @@ class Timedelta(_Timedelta):
"milliseconds", "microseconds", "nanoseconds"}

def __new__(cls, object value=_no_input, unit=None, **kwargs):
unsupported_kwargs = set(kwargs)
unsupported_kwargs.difference_update(cls._req_any_kwargs_new)
if unsupported_kwargs or (
value is _no_input and
not cls._req_any_kwargs_new.intersection(kwargs)
):
raise ValueError(
# GH#53801
"cannot construct a Timedelta from the passed arguments, "
"allowed keywords are "
"[weeks, days, hours, minutes, seconds, "
"milliseconds, microseconds, nanoseconds]"
)

if value is _no_input:
if not len(kwargs):
raise ValueError("cannot construct a Timedelta without a "
Expand All @@ -2014,16 +2028,6 @@ class Timedelta(_Timedelta):

kwargs = {key: _to_py_int_float(kwargs[key]) for key in kwargs}

unsupported_kwargs = set(kwargs)
unsupported_kwargs.difference_update(cls._req_any_kwargs_new)
if unsupported_kwargs or not cls._req_any_kwargs_new.intersection(kwargs):
raise ValueError(
"cannot construct a Timedelta from the passed arguments, "
"allowed keywords are "
"[weeks, days, hours, minutes, seconds, "
"milliseconds, microseconds, nanoseconds]"
)

# GH43764, convert any input to nanoseconds first and then
# create the timedelta. This ensures that any potential
# nanosecond contributions from kwargs parsed as floats
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/tslibs/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def test_kwarg_assertion(kwargs):
with pytest.raises(ValueError, match=re.escape(err_message)):
Timedelta(**kwargs)

with pytest.raises(ValueError, match=re.escape(err_message)):
# GH#53801 'unit' misspelled as 'units'
Timedelta(1, units="hours")


class TestArrayToTimedelta64:
def test_array_to_timedelta64_string_with_unit_2d_raises(self):
Expand Down
Loading