@@ -319,10 +319,10 @@ def test_dti_tz_localize_nonexistent_raise_coerce(self):
319
319
times = ["2015-03-08 01:00" , "2015-03-08 02:00" , "2015-03-08 03:00" ]
320
320
index = DatetimeIndex (times )
321
321
tz = "US/Eastern"
322
- with pytest .raises (pytz .NonExistentTimeError ):
322
+ with pytest .raises (pytz .NonExistentTimeError , match = "|" . join ( times ) ):
323
323
index .tz_localize (tz = tz )
324
324
325
- with pytest .raises (pytz .NonExistentTimeError ):
325
+ with pytest .raises (pytz .NonExistentTimeError , match = "|" . join ( times ) ):
326
326
index .tz_localize (tz = tz , nonexistent = "raise" )
327
327
328
328
result = index .tz_localize (tz = tz , nonexistent = "NaT" )
@@ -336,7 +336,7 @@ def test_dti_tz_localize_ambiguous_infer(self, tz):
336
336
# November 6, 2011, fall back, repeat 2 AM hour
337
337
# With no repeated hours, we cannot infer the transition
338
338
dr = date_range (datetime (2011 , 11 , 6 , 0 ), periods = 5 , freq = pd .offsets .Hour ())
339
- with pytest .raises (pytz .AmbiguousTimeError ):
339
+ with pytest .raises (pytz .AmbiguousTimeError , match = "Cannot infer dst time" ):
340
340
dr .tz_localize (tz )
341
341
342
342
# With repeated hours, we can infer the transition
@@ -365,7 +365,7 @@ def test_dti_tz_localize_ambiguous_infer(self, tz):
365
365
def test_dti_tz_localize_ambiguous_times (self , tz ):
366
366
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
367
367
dr = date_range (datetime (2011 , 3 , 13 , 1 , 30 ), periods = 3 , freq = pd .offsets .Hour ())
368
- with pytest .raises (pytz .NonExistentTimeError ):
368
+ with pytest .raises (pytz .NonExistentTimeError , match = "2011-03-13 02:30:00" ):
369
369
dr .tz_localize (tz )
370
370
371
371
# after dst transition, it works
@@ -375,7 +375,7 @@ def test_dti_tz_localize_ambiguous_times(self, tz):
375
375
376
376
# November 6, 2011, fall back, repeat 2 AM hour
377
377
dr = date_range (datetime (2011 , 11 , 6 , 1 , 30 ), periods = 3 , freq = pd .offsets .Hour ())
378
- with pytest .raises (pytz .AmbiguousTimeError ):
378
+ with pytest .raises (pytz .AmbiguousTimeError , match = "Cannot infer dst time" ):
379
379
dr .tz_localize (tz )
380
380
381
381
# UTC is OK
@@ -411,11 +411,11 @@ def test_dti_tz_localize(self, prefix):
411
411
tm .assert_numpy_array_equal (dti3 .values , dti_utc .values )
412
412
413
413
dti = pd .date_range (start = "11/6/2011 1:59" , end = "11/6/2011 2:00" , freq = "L" )
414
- with pytest .raises (pytz .AmbiguousTimeError ):
414
+ with pytest .raises (pytz .AmbiguousTimeError , match = "Cannot infer dst time" ):
415
415
dti .tz_localize (tzstr )
416
416
417
417
dti = pd .date_range (start = "3/13/2011 1:59" , end = "3/13/2011 2:00" , freq = "L" )
418
- with pytest .raises (pytz .NonExistentTimeError ):
418
+ with pytest .raises (pytz .NonExistentTimeError , match = "2011-03-13 02:00:00" ):
419
419
dti .tz_localize (tzstr )
420
420
421
421
@pytest .mark .parametrize (
@@ -441,7 +441,7 @@ def test_dti_tz_localize_utc_conversion(self, tz):
441
441
# DST ambiguity, this should fail
442
442
rng = date_range ("3/11/2012" , "3/12/2012" , freq = "30T" )
443
443
# Is this really how it should fail??
444
- with pytest .raises (pytz .NonExistentTimeError ):
444
+ with pytest .raises (pytz .NonExistentTimeError , match = "2012-03-11 02:00:00" ):
445
445
rng .tz_localize (tz )
446
446
447
447
def test_dti_tz_localize_roundtrip (self , tz_aware_fixture ):
@@ -452,7 +452,9 @@ def test_dti_tz_localize_roundtrip(self, tz_aware_fixture):
452
452
tz = tz_aware_fixture
453
453
localized = idx .tz_localize (tz )
454
454
# cant localize a tz-aware object
455
- with pytest .raises (TypeError ):
455
+ with pytest .raises (
456
+ TypeError , match = "Already tz-aware, use tz_convert to convert"
457
+ ):
456
458
localized .tz_localize (tz )
457
459
reset = localized .tz_localize (None )
458
460
assert reset .tzinfo is None
@@ -542,7 +544,8 @@ def test_dti_tz_localize_ambiguous_flags(self, tz):
542
544
di = DatetimeIndex (times )
543
545
544
546
# When the sizes are incompatible, make sure error is raised
545
- with pytest .raises (Exception ):
547
+ msg = "Length of ambiguous bool-array must be the same size as vals"
548
+ with pytest .raises (Exception , match = msg ):
546
549
di .tz_localize (tz , ambiguous = is_dst )
547
550
548
551
# When sizes are compatible and there are repeats ('infer' won't work)
@@ -564,7 +567,7 @@ def test_dti_construction_ambiguous_endpoint(self, tz):
564
567
# construction with an ambiguous end-point
565
568
# GH#11626
566
569
567
- with pytest .raises (pytz .AmbiguousTimeError ):
570
+ with pytest .raises (pytz .AmbiguousTimeError , match = "Cannot infer dst time" ):
568
571
date_range (
569
572
"2013-10-26 23:00" , "2013-10-27 01:00" , tz = "Europe/London" , freq = "H"
570
573
)
@@ -588,7 +591,7 @@ def test_dti_construction_ambiguous_endpoint(self, tz):
588
591
def test_dti_construction_nonexistent_endpoint (self , tz , option , expected ):
589
592
# construction with an nonexistent end-point
590
593
591
- with pytest .raises (pytz .NonExistentTimeError ):
594
+ with pytest .raises (pytz .NonExistentTimeError , match = "2019-03-10 02:00:00" ):
592
595
date_range (
593
596
"2019-03-10 00:00" , "2019-03-10 02:00" , tz = "US/Pacific" , freq = "H"
594
597
)
@@ -613,10 +616,15 @@ def test_dti_tz_localize_nonexistent(self, tz, method, exp):
613
616
n = 60
614
617
dti = date_range (start = "2015-03-29 02:00:00" , periods = n , freq = "min" )
615
618
if method == "raise" :
616
- with pytest .raises (pytz .NonExistentTimeError ):
619
+ with pytest .raises (pytz .NonExistentTimeError , match = "2015-03-29 02:00:00" ):
617
620
dti .tz_localize (tz , nonexistent = method )
618
621
elif exp == "invalid" :
619
- with pytest .raises (ValueError ):
622
+ msg = (
623
+ "The nonexistent argument must be one of "
624
+ "'raise', 'NaT', 'shift_forward', 'shift_backward' "
625
+ "or a timedelta object"
626
+ )
627
+ with pytest .raises (ValueError , match = msg ):
620
628
dti .tz_localize (tz , nonexistent = method )
621
629
else :
622
630
result = dti .tz_localize (tz , nonexistent = method )
@@ -1082,7 +1090,8 @@ def test_with_tz(self, tz):
1082
1090
dr = bdate_range (
1083
1091
datetime (2005 , 1 , 1 , tzinfo = pytz .utc ), datetime (2009 , 1 , 1 , tzinfo = pytz .utc )
1084
1092
)
1085
- with pytest .raises (Exception ):
1093
+ msg = "Start and end cannot both be tz-aware with different timezones"
1094
+ with pytest .raises (Exception , match = msg ):
1086
1095
bdate_range (datetime (2005 , 1 , 1 , tzinfo = pytz .utc ), "1/1/2009" , tz = tz )
1087
1096
1088
1097
@pytest .mark .parametrize ("prefix" , ["" , "dateutil/" ])
0 commit comments