8
8
import numpy as np
9
9
import pytest
10
10
11
+ from pandas ._libs .tslibs import OutOfBoundsTimedelta
12
+
11
13
from pandas import (
12
14
NA ,
13
15
NaT ,
@@ -163,16 +165,19 @@ def test_construction():
163
165
164
166
165
167
@pytest .mark .parametrize ("unit" , ("ps" , "ns" ))
166
- def test_from_np_td64_ignores_unit (unit : str , timedelta_overflow ):
168
+ def test_from_np_td64_ignores_unit (unit : str ):
167
169
"""
168
170
Ignore the unit, as it may cause silently overflows leading to incorrect results,
169
171
and in non-overflow cases is irrelevant GH#46827.
170
172
"""
171
173
td64 = np .timedelta64 (NP_TD64_MAX_PER_UNIT ["h" ], "h" )
174
+ msg = re .escape (
175
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
176
+ )
172
177
173
178
assert Timedelta (td64 , unit = unit ) == Timedelta (td64 )
174
179
175
- with pytest .raises (** timedelta_overflow ):
180
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
176
181
Timedelta (td64 * 2 , unit = unit )
177
182
178
183
@@ -448,13 +453,7 @@ def test_raises_if_ambiguous_units_passed(self, unit: str):
448
453
449
454
class TestOverflow :
450
455
@pytest .mark .parametrize (("unit" , "max_val" ), TD_MAX_PER_UNIT .items ())
451
- def test_int_plus_units_too_big (
452
- self ,
453
- unit : str ,
454
- max_val : int ,
455
- request ,
456
- timedelta_overflow ,
457
- ):
456
+ def test_int_plus_units_too_big (self , unit : str , max_val : int , request ):
458
457
if unit == "w" :
459
458
mark = pytest .mark .xfail (
460
459
reason = "does not raise" ,
@@ -464,18 +463,15 @@ def test_int_plus_units_too_big(
464
463
request .node .add_marker (mark )
465
464
466
465
too_big = max_val + 1
466
+ msg = re .escape (
467
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
468
+ )
467
469
468
- with pytest .raises (** timedelta_overflow ):
470
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
469
471
Timedelta (too_big , unit = unit )
470
472
471
473
@pytest .mark .parametrize (("unit" , "min_val" ), skip_ns (TD_MIN_PER_UNIT ).items ())
472
- def test_int_plus_units_too_small (
473
- self ,
474
- unit : str ,
475
- min_val : int ,
476
- request ,
477
- timedelta_overflow ,
478
- ):
474
+ def test_int_plus_units_too_small (self , unit : str , min_val : int , request ):
479
475
if unit == "w" :
480
476
mark = pytest .mark .xfail (
481
477
reason = "does not raise" ,
@@ -485,55 +481,71 @@ def test_int_plus_units_too_small(
485
481
request .node .add_marker (mark )
486
482
487
483
too_small = min_val - 1
484
+ msg = re .escape (
485
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
486
+ )
488
487
489
- with pytest .raises (** timedelta_overflow ):
488
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
490
489
Timedelta (too_small , unit = unit )
491
490
492
491
@pytest .mark .parametrize (("kwarg" , "max_val" ), TD_MAX_PER_KWARG .items ())
493
- def test_kwarg_too_big (self , kwarg : str , max_val : int , timedelta_overflow ):
492
+ def test_kwarg_too_big (self , kwarg : str , max_val : int ):
494
493
too_big = max_val + 1
494
+ msg = re .escape (
495
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
496
+ )
495
497
496
- with pytest .raises (** timedelta_overflow ):
498
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
497
499
assert Timedelta (** {kwarg : too_big }) # type: ignore[arg-type]
498
500
499
501
@pytest .mark .parametrize (("kwarg" , "min_val" ), skip_ns (TD_MIN_PER_KWARG ).items ())
500
- def test_kwarg_too_small (self , kwarg : str , min_val : int , timedelta_overflow ):
502
+ def test_kwarg_too_small (self , kwarg : str , min_val : int ):
501
503
too_small = min_val - 1
504
+ msg = re .escape (
505
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
506
+ )
502
507
503
- with pytest .raises (** timedelta_overflow ):
508
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
504
509
Timedelta (** {kwarg : too_small }) # type: ignore[arg-type]
505
510
506
511
@pytest .mark .parametrize (("kwarg" , "max_val" ), skip_ns (TD_MAX_PER_KWARG ).items ())
507
- def test_from_timedelta_too_big (self , kwarg : str , max_val : int , timedelta_overflow ):
512
+ def test_from_timedelta_too_big (self , kwarg : str , max_val : int ):
508
513
too_big = timedelta (** {kwarg : max_val + 1 })
514
+ msg = re .escape (
515
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
516
+ )
509
517
510
- with pytest .raises (** timedelta_overflow ):
518
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
511
519
Timedelta (too_big )
512
520
513
521
@pytest .mark .parametrize (("kwarg" , "min_val" ), skip_ns (TD_MIN_PER_KWARG ).items ())
514
- def test_from_timedelta_too_small (
515
- self ,
516
- kwarg : str ,
517
- min_val : int ,
518
- timedelta_overflow ,
519
- ):
522
+ def test_from_timedelta_too_small (self , kwarg : str , min_val : int ):
520
523
too_small = timedelta (** {kwarg : min_val - 1 })
524
+ msg = re .escape (
525
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
526
+ )
521
527
522
- with pytest .raises (** timedelta_overflow ):
528
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
523
529
Timedelta (too_small )
524
530
525
531
@pytest .mark .parametrize (("unit" , "max_val" ), skip_ns (NP_TD64_MAX_PER_UNIT ).items ())
526
- def test_from_np_td64_too_big (self , unit : str , max_val : int , timedelta_overflow ):
532
+ def test_from_np_td64_too_big (self , unit : str , max_val : int ):
527
533
too_big = np .timedelta64 (max_val + 1 , unit )
534
+ msg = re .escape (
535
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
536
+ )
528
537
529
- with pytest .raises (** timedelta_overflow ):
538
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
530
539
Timedelta (too_big )
531
540
532
541
@pytest .mark .parametrize (("unit" , "min_val" ), skip_ns (NP_TD64_MIN_PER_UNIT ).items ())
533
- def test_from_np_td64_too_small (self , unit : str , min_val : int , timedelta_overflow ):
542
+ def test_from_np_td64_too_small (self , unit : str , min_val : int ):
534
543
too_small = np .timedelta64 (min_val - 1 , unit )
544
+ msg = re .escape (
545
+ "outside allowed range [-9223372036854775807ns, 9223372036854775807ns]"
546
+ )
535
547
536
- with pytest .raises (** timedelta_overflow ):
548
+ with pytest .raises (OutOfBoundsTimedelta , match = msg ):
537
549
Timedelta (too_small )
538
550
539
551
def test_too_small_by_1ns_returns_nat (self ):
0 commit comments