From 7bbe4a50bbe1d4d98094143970e0033824ce9ad1 Mon Sep 17 00:00:00 2001 From: Joeperdefloep Date: Fri, 25 Jun 2021 20:47:31 +0200 Subject: [PATCH 1/2] BUG: added test #36615 --- pandas/tests/tslibs/test_timedeltas.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index 25450bd64a298..aebdb36271327 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -8,6 +8,8 @@ offsets, ) +from pandas.errors import OutOfBoundsTimedelta + @pytest.mark.parametrize( "obj,expected", @@ -37,3 +39,9 @@ def test_huge_nanoseconds_overflow(): # GH 32402 assert delta_to_nanoseconds(Timedelta(1e10)) == 1e10 assert delta_to_nanoseconds(Timedelta(nanoseconds=1e10)) == 1e10 + + +def test_out_of_bounds(): + # GH 36615 + with pytest.raises(OutOfBoundsTimedelta, match="200000 days"): + Timedelta(np.timedelta64(200000, "D")) From 9797e4310e43e7ca7bb84e682923c659a41b7fba Mon Sep 17 00:00:00 2001 From: Joeperdefloep Date: Sat, 26 Jun 2021 11:19:58 +0200 Subject: [PATCH 2/2] moved and parametrized test --- .../scalar/timedelta/test_constructors.py | 19 +++++++++++++++---- pandas/tests/tslibs/test_timedeltas.py | 8 -------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pandas/tests/scalar/timedelta/test_constructors.py b/pandas/tests/scalar/timedelta/test_constructors.py index ea4a56be6da48..34b725eb9fe77 100644 --- a/pandas/tests/scalar/timedelta/test_constructors.py +++ b/pandas/tests/scalar/timedelta/test_constructors.py @@ -204,15 +204,26 @@ def test_overflow_on_construction(): Timedelta(timedelta(days=13 * 19999)) -def test_construction_out_of_bounds_td64(): +@pytest.mark.parametrize( + "val, unit, name", + [ + (3508, "M", " months"), + (15251, "W", " weeks"), # 1 + (106752, "D", " days"), # change from previous: + (2562048, "h", " hours"), # 0 hours + (153722868, "m", " minutes"), # 13 minutes + (9223372037, "s", " seconds"), # 44 seconds + ], +) +def test_construction_out_of_bounds_td64(val, unit, name): # TODO: parametrize over units just above/below the implementation bounds # once GH#38964 is resolved # Timedelta.max is just under 106752 days - td64 = np.timedelta64(106752, "D") + td64 = np.timedelta64(val, unit) assert td64.astype("m8[ns]").view("i8") < 0 # i.e. naive astype will be wrong - msg = "106752 days" + msg = str(val) + name with pytest.raises(OutOfBoundsTimedelta, match=msg): Timedelta(td64) @@ -222,7 +233,7 @@ def test_construction_out_of_bounds_td64(): td64 *= -1 assert td64.astype("m8[ns]").view("i8") > 0 # i.e. naive astype will be wrong - with pytest.raises(OutOfBoundsTimedelta, match=msg): + with pytest.raises(OutOfBoundsTimedelta, match="-" + msg): Timedelta(td64) # But just back in bounds and we are OK diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py index aebdb36271327..25450bd64a298 100644 --- a/pandas/tests/tslibs/test_timedeltas.py +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -8,8 +8,6 @@ offsets, ) -from pandas.errors import OutOfBoundsTimedelta - @pytest.mark.parametrize( "obj,expected", @@ -39,9 +37,3 @@ def test_huge_nanoseconds_overflow(): # GH 32402 assert delta_to_nanoseconds(Timedelta(1e10)) == 1e10 assert delta_to_nanoseconds(Timedelta(nanoseconds=1e10)) == 1e10 - - -def test_out_of_bounds(): - # GH 36615 - with pytest.raises(OutOfBoundsTimedelta, match="200000 days"): - Timedelta(np.timedelta64(200000, "D"))