Skip to content

Commit 3e9237c

Browse files
authored
BUG: Timedelta with invalid keyword (#61883)
1 parent 042ac78 commit 3e9237c

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ Datetimelike
717717
Timedelta
718718
^^^^^^^^^
719719
- Accuracy improvement in :meth:`Timedelta.to_pytimedelta` to round microseconds consistently for large nanosecond based Timedelta (:issue:`57841`)
720+
- Bug in :class:`Timedelta` constructor failing to raise when passed an invalid keyword (:issue:`53801`)
720721
- Bug in :meth:`DataFrame.cumsum` which was raising ``IndexError`` if dtype is ``timedelta64[ns]`` (:issue:`57956`)
721722

722723
Timezones

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,20 @@ class Timedelta(_Timedelta):
20062006
"milliseconds", "microseconds", "nanoseconds"}
20072007

20082008
def __new__(cls, object value=_no_input, unit=None, **kwargs):
2009+
unsupported_kwargs = set(kwargs)
2010+
unsupported_kwargs.difference_update(cls._req_any_kwargs_new)
2011+
if unsupported_kwargs or (
2012+
value is _no_input and
2013+
not cls._req_any_kwargs_new.intersection(kwargs)
2014+
):
2015+
raise ValueError(
2016+
# GH#53801
2017+
"cannot construct a Timedelta from the passed arguments, "
2018+
"allowed keywords are "
2019+
"[weeks, days, hours, minutes, seconds, "
2020+
"milliseconds, microseconds, nanoseconds]"
2021+
)
2022+
20092023
if value is _no_input:
20102024
if not len(kwargs):
20112025
raise ValueError("cannot construct a Timedelta without a "
@@ -2014,16 +2028,6 @@ class Timedelta(_Timedelta):
20142028

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

2017-
unsupported_kwargs = set(kwargs)
2018-
unsupported_kwargs.difference_update(cls._req_any_kwargs_new)
2019-
if unsupported_kwargs or not cls._req_any_kwargs_new.intersection(kwargs):
2020-
raise ValueError(
2021-
"cannot construct a Timedelta from the passed arguments, "
2022-
"allowed keywords are "
2023-
"[weeks, days, hours, minutes, seconds, "
2024-
"milliseconds, microseconds, nanoseconds]"
2025-
)
2026-
20272031
# GH43764, convert any input to nanoseconds first and then
20282032
# create the timedelta. This ensures that any potential
20292033
# nanosecond contributions from kwargs parsed as floats

pandas/tests/tslibs/test_timedeltas.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def test_kwarg_assertion(kwargs):
104104
with pytest.raises(ValueError, match=re.escape(err_message)):
105105
Timedelta(**kwargs)
106106

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

108112
class TestArrayToTimedelta64:
109113
def test_array_to_timedelta64_string_with_unit_2d_raises(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy