@@ -135,8 +135,10 @@ def test_construction_with_alt_tz_localize(self, kwargs, tz_aware_fixture):
135135 tm .assert_index_equal (i2 , expected )
136136
137137 # incompat tz/dtype
138- pytest .raises (ValueError , lambda : DatetimeIndex (
139- i .tz_localize (None ).asi8 , dtype = i .dtype , tz = 'US/Pacific' ))
138+ msg = "cannot supply both a tz and a dtype with a tz"
139+ with pytest .raises (ValueError , match = msg ):
140+ DatetimeIndex (i .tz_localize (None ).asi8 ,
141+ dtype = i .dtype , tz = 'US/Pacific' )
140142
141143 def test_construction_index_with_mixed_timezones (self ):
142144 # gh-11488: no tz results in DatetimeIndex
@@ -439,14 +441,19 @@ def test_constructor_coverage(self):
439441 tm .assert_index_equal (from_ints , expected )
440442
441443 # non-conforming
442- pytest .raises (ValueError , DatetimeIndex ,
443- ['2000-01-01' , '2000-01-02' , '2000-01-04' ], freq = 'D' )
444+ msg = ("Inferred frequency None from passed values does not conform"
445+ " to passed frequency D" )
446+ with pytest .raises (ValueError , match = msg ):
447+ DatetimeIndex (['2000-01-01' , '2000-01-02' , '2000-01-04' ], freq = 'D' )
444448
445- pytest .raises (ValueError , date_range , start = '2011-01-01' ,
446- freq = 'b' )
447- pytest .raises (ValueError , date_range , end = '2011-01-01' ,
448- freq = 'B' )
449- pytest .raises (ValueError , date_range , periods = 10 , freq = 'D' )
449+ msg = ("Of the four parameters: start, end, periods, and freq, exactly"
450+ " three must be specified" )
451+ with pytest .raises (ValueError , match = msg ):
452+ date_range (start = '2011-01-01' , freq = 'b' )
453+ with pytest .raises (ValueError , match = msg ):
454+ date_range (end = '2011-01-01' , freq = 'B' )
455+ with pytest .raises (ValueError , match = msg ):
456+ date_range (periods = 10 , freq = 'D' )
450457
451458 @pytest .mark .parametrize ('freq' , ['AS' , 'W-SUN' ])
452459 def test_constructor_datetime64_tzformat (self , freq ):
@@ -511,18 +518,20 @@ def test_constructor_dtype(self):
511518 idx = DatetimeIndex (['2013-01-01' , '2013-01-02' ],
512519 dtype = 'datetime64[ns, US/Eastern]' )
513520
514- pytest .raises (ValueError ,
515- lambda : DatetimeIndex (idx ,
516- dtype = 'datetime64[ns]' ))
521+ msg = ("cannot supply both a tz and a timezone-naive dtype"
522+ r" \(i\.e\. datetime64\[ns\]\)" )
523+ with pytest .raises (ValueError , match = msg ):
524+ DatetimeIndex (idx , dtype = 'datetime64[ns]' )
517525
518526 # this is effectively trying to convert tz's
519- pytest .raises (TypeError ,
520- lambda : DatetimeIndex (idx ,
521- dtype = 'datetime64[ns, CET]' ))
522- pytest .raises (ValueError ,
523- lambda : DatetimeIndex (
524- idx , tz = 'CET' ,
525- dtype = 'datetime64[ns, US/Eastern]' ))
527+ msg = ("data is already tz-aware US/Eastern, unable to set specified"
528+ " tz: CET" )
529+ with pytest .raises (TypeError , match = msg ):
530+ DatetimeIndex (idx , dtype = 'datetime64[ns, CET]' )
531+ msg = "cannot supply both a tz and a dtype with a tz"
532+ with pytest .raises (ValueError , match = msg ):
533+ DatetimeIndex (idx , tz = 'CET' , dtype = 'datetime64[ns, US/Eastern]' )
534+
526535 result = DatetimeIndex (idx , dtype = 'datetime64[ns, US/Eastern]' )
527536 tm .assert_index_equal (idx , result )
528537
@@ -732,7 +741,9 @@ def test_from_freq_recreate_from_data(self, freq):
732741
733742 def test_datetimeindex_constructor_misc (self ):
734743 arr = ['1/1/2005' , '1/2/2005' , 'Jn 3, 2005' , '2005-01-04' ]
735- pytest .raises (Exception , DatetimeIndex , arr )
744+ msg = r"(\(u?')?Unknown string format(:', 'Jn 3, 2005'\))?"
745+ with pytest .raises (ValueError , match = msg ):
746+ DatetimeIndex (arr )
736747
737748 arr = ['1/1/2005' , '1/2/2005' , '1/3/2005' , '2005-01-04' ]
738749 idx1 = DatetimeIndex (arr )
0 commit comments