Skip to content

Commit e681fcd

Browse files
authored
BUG: raise on wrong keyword arguments in Timedelta (pandas-dev#45227)
1 parent ec79b2b commit e681fcd

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ Timedelta
734734
^^^^^^^^^
735735
- 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`)
736736
- Bug in floor division of ``timedelta64[ns]`` data with a scalar returning garbage values (:issue:`44466`)
737-
- Bug in :class:`Timedelta` now properly taking into account any nanoseconds contribution of any kwarg (:issue:`43764`)
737+
- Bug in :class:`Timedelta` now properly taking into account any nanoseconds contribution of any kwarg (:issue:`43764`, :issue:`45227`)
738738

739739
Timezones
740740
^^^^^^^^^

doc/source/whatsnew/v1.5.0.rst

-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ Datetimelike
124124
Timedelta
125125
^^^^^^^^^
126126
-
127-
-
128127

129128
Timezones
130129
^^^^^^^^^

pandas/_libs/tslibs/timedeltas.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,10 @@ class Timedelta(_Timedelta):
12891289
"(days,seconds....)")
12901290

12911291
kwargs = {key: _to_py_int_float(kwargs[key]) for key in kwargs}
1292-
if not cls._req_any_kwargs_new.intersection(kwargs):
1292+
1293+
unsupported_kwargs = set(kwargs)
1294+
unsupported_kwargs.difference_update(cls._req_any_kwargs_new)
1295+
if unsupported_kwargs or not cls._req_any_kwargs_new.intersection(kwargs):
12931296
raise ValueError(
12941297
"cannot construct a Timedelta from the passed arguments, "
12951298
"allowed keywords are "

pandas/tests/tslibs/test_timedeltas.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -46,3 +48,18 @@ def test_huge_nanoseconds_overflow():
4648
# GH 32402
4749
assert delta_to_nanoseconds(Timedelta(1e10)) == 1e10
4850
assert delta_to_nanoseconds(Timedelta(nanoseconds=1e10)) == 1e10
51+
52+
53+
@pytest.mark.parametrize(
54+
"kwargs", [{"Seconds": 1}, {"seconds": 1, "Nanoseconds": 1}, {"Foo": 2}]
55+
)
56+
def test_kwarg_assertion(kwargs):
57+
err_message = (
58+
"cannot construct a Timedelta from the passed arguments, "
59+
"allowed keywords are "
60+
"[weeks, days, hours, minutes, seconds, "
61+
"milliseconds, microseconds, nanoseconds]"
62+
)
63+
64+
with pytest.raises(ValueError, match=re.escape(err_message)):
65+
Timedelta(**kwargs)

0 commit comments

Comments
 (0)