From 470db782cc27fe0e2d1923961c12a4518b8276ee Mon Sep 17 00:00:00 2001 From: deponovo Date: Thu, 6 Jan 2022 17:18:35 +0100 Subject: [PATCH 1/6] BUG: raise on wrong keyword arguments in Timedelta TST: added respective testing --- pandas/_libs/tslibs/timedeltas.pyx | 5 ++++- pandas/tests/tslibs/test_timedeltas.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 02017c5900ca0..de59f1634d9f3 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1270,7 +1270,10 @@ class Timedelta(_Timedelta): "(days,seconds....)") kwargs = {key: _to_py_int_float(kwargs[key]) for key in kwargs} - if not cls._req_any_kwargs_new.intersection(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 " diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index c72d279580cca..534f6d72c2dfb 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -46,3 +46,12 @@ def test_huge_nanoseconds_overflow(): # GH 32402 assert delta_to_nanoseconds(Timedelta(1e10)) == 1e10 assert delta_to_nanoseconds(Timedelta(nanoseconds=1e10)) == 1e10 + + +@pytest.mark.parametrize( + "kwargs", + [{"Seconds": 1}, {"seconds": 1, "Nanoseconds": 1}, {"Foo": 2}] +) +def test_kwarg_assertion(kwargs): + with pytest.raises(ValueError): + Timedelta(**kwargs) From 8564484f951d33cc2cc01b33dcd760bf99de7fd4 Mon Sep 17 00:00:00 2001 From: deponovo Date: Thu, 6 Jan 2022 17:23:39 +0100 Subject: [PATCH 2/6] DOC: added entry to whatsnew --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 7f2a1a9305039..5fa51da07a968 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -121,7 +121,7 @@ Datetimelike Timedelta ^^^^^^^^^ -- +- Now raising on missing or invalid kwargs (:issue:`43764`) - Timezones From ad866582482c6c2aefed46db4d679d7a5b886555 Mon Sep 17 00:00:00 2001 From: deponovo Date: Fri, 7 Jan 2022 15:57:45 +0100 Subject: [PATCH 3/6] TST: fixed missing `match` kwarg required by pre-commit --- pandas/tests/tslibs/test_timedeltas.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index 534f6d72c2dfb..a25f148131ea0 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -1,3 +1,5 @@ +import re + import numpy as np import pytest @@ -49,9 +51,15 @@ def test_huge_nanoseconds_overflow(): @pytest.mark.parametrize( - "kwargs", - [{"Seconds": 1}, {"seconds": 1, "Nanoseconds": 1}, {"Foo": 2}] + "kwargs", [{"Seconds": 1}, {"seconds": 1, "Nanoseconds": 1}, {"Foo": 2}] ) def test_kwarg_assertion(kwargs): - with pytest.raises(ValueError): + err_message = ( + "cannot construct a Timedelta from the passed arguments, " + "allowed keywords are " + "[weeks, days, hours, minutes, seconds, " + "milliseconds, microseconds, nanoseconds]" + ) + + with pytest.raises(ValueError, match=re.escape(err_message)): Timedelta(**kwargs) From df79514651e560ddedb16cd491f8046ec62818c0 Mon Sep 17 00:00:00 2001 From: deponovo Date: Sat, 8 Jan 2022 17:14:45 +0100 Subject: [PATCH 4/6] DOC: moved whatsnew entry from v1.5.0 to v1.4.0 --- doc/source/whatsnew/v1.4.0.rst | 1 + doc/source/whatsnew/v1.5.0.rst | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 3ee34a158902b..68d5fa356d9f2 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -734,6 +734,7 @@ Timedelta - 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`) - Bug in floor division of ``timedelta64[ns]`` data with a scalar returning garbage values (:issue:`44466`) - Bug in :class:`Timedelta` now properly taking into account any nanoseconds contribution of any kwarg (:issue:`43764`) +- Now raising on missing or invalid kwargs (:issue:`43764`) Timezones ^^^^^^^^^ diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 5fa51da07a968..f58757c5b3c70 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -121,7 +121,6 @@ Datetimelike Timedelta ^^^^^^^^^ -- Now raising on missing or invalid kwargs (:issue:`43764`) - Timezones From d321bfb5546af08470655e46ec54ec29e44668d6 Mon Sep 17 00:00:00 2001 From: deponovo Date: Sat, 8 Jan 2022 17:25:24 +0100 Subject: [PATCH 5/6] DOC: added suggestion from review --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 68d5fa356d9f2..dd42b6d607c12 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -734,7 +734,7 @@ Timedelta - 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`) - Bug in floor division of ``timedelta64[ns]`` data with a scalar returning garbage values (:issue:`44466`) - Bug in :class:`Timedelta` now properly taking into account any nanoseconds contribution of any kwarg (:issue:`43764`) -- Now raising on missing or invalid kwargs (:issue:`43764`) +- Now raising on missing or invalid kwargs (:issue:`43764`, :issue:`45227`) Timezones ^^^^^^^^^ From f50c7c6286c36e5c7dca366db2d1697dfcda2c0e Mon Sep 17 00:00:00 2001 From: deponovo Date: Sat, 8 Jan 2022 17:28:28 +0100 Subject: [PATCH 6/6] DOC: fixed misunderstanding --- doc/source/whatsnew/v1.4.0.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index dd42b6d607c12..8b4c5220a385a 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -733,8 +733,7 @@ Timedelta ^^^^^^^^^ - 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`) - Bug in floor division of ``timedelta64[ns]`` data with a scalar returning garbage values (:issue:`44466`) -- Bug in :class:`Timedelta` now properly taking into account any nanoseconds contribution of any kwarg (:issue:`43764`) -- Now raising on missing or invalid kwargs (:issue:`43764`, :issue:`45227`) +- Bug in :class:`Timedelta` now properly taking into account any nanoseconds contribution of any kwarg (:issue:`43764`, :issue:`45227`) Timezones ^^^^^^^^^