11import numpy as np
22import pytest
33
4+ from pandas ._libs .tslibs .period import IncompatibleFrequency
45from pandas .compat import PY3 , lmap , lrange , text_type
56
67from pandas .core .dtypes .dtypes import PeriodDtype
@@ -66,12 +67,17 @@ def test_constructor_field_arrays(self):
6667
6768 years = [2007 , 2007 , 2007 ]
6869 months = [1 , 2 ]
69- pytest .raises (ValueError , PeriodIndex , year = years , month = months ,
70- freq = 'M' )
71- pytest .raises (ValueError , PeriodIndex , year = years , month = months ,
72- freq = '2M' )
73- pytest .raises (ValueError , PeriodIndex , year = years , month = months ,
74- freq = 'M' , start = Period ('2007-01' , freq = 'M' ))
70+
71+ msg = "Mismatched Period array lengths"
72+ with pytest .raises (ValueError , match = msg ):
73+ PeriodIndex (year = years , month = months , freq = 'M' )
74+ with pytest .raises (ValueError , match = msg ):
75+ PeriodIndex (year = years , month = months , freq = '2M' )
76+
77+ msg = "Can either instantiate from fields or endpoints, but not both"
78+ with pytest .raises (ValueError , match = msg ):
79+ PeriodIndex (year = years , month = months , freq = 'M' ,
80+ start = Period ('2007-01' , freq = 'M' ))
7581
7682 years = [2007 , 2007 , 2007 ]
7783 months = [1 , 2 , 3 ]
@@ -81,8 +87,8 @@ def test_constructor_field_arrays(self):
8187
8288 def test_constructor_U (self ):
8389 # U was used as undefined period
84- pytest .raises (ValueError , period_range , '2007-1-1' , periods = 500 ,
85- freq = 'X' )
90+ with pytest .raises (ValueError , match = "Invalid frequency: X" ):
91+ period_range ( '2007-1-1' , periods = 500 , freq = 'X' )
8692
8793 def test_constructor_nano (self ):
8894 idx = period_range (start = Period (ordinal = 1 , freq = 'N' ),
@@ -103,17 +109,29 @@ def test_constructor_arrays_negative_year(self):
103109 tm .assert_index_equal (pindex .quarter , pd .Index (quarters ))
104110
105111 def test_constructor_invalid_quarters (self ):
106- pytest .raises (ValueError , PeriodIndex , year = lrange (2000 , 2004 ),
107- quarter = lrange (4 ), freq = 'Q-DEC' )
112+ msg = "Quarter must be 1 <= q <= 4"
113+ with pytest .raises (ValueError , match = msg ):
114+ PeriodIndex (year = lrange (2000 , 2004 ), quarter = lrange (4 ),
115+ freq = 'Q-DEC' )
108116
109117 def test_constructor_corner (self ):
110- pytest .raises (ValueError , PeriodIndex , periods = 10 , freq = 'A' )
118+ msg = "Not enough parameters to construct Period range"
119+ with pytest .raises (ValueError , match = msg ):
120+ PeriodIndex (periods = 10 , freq = 'A' )
111121
112122 start = Period ('2007' , freq = 'A-JUN' )
113123 end = Period ('2010' , freq = 'A-DEC' )
114- pytest .raises (ValueError , PeriodIndex , start = start , end = end )
115- pytest .raises (ValueError , PeriodIndex , start = start )
116- pytest .raises (ValueError , PeriodIndex , end = end )
124+
125+ msg = "start and end must have same freq"
126+ with pytest .raises (ValueError , match = msg ):
127+ PeriodIndex (start = start , end = end )
128+
129+ msg = ("Of the three parameters: start, end, and periods, exactly two"
130+ " must be specified" )
131+ with pytest .raises (ValueError , match = msg ):
132+ PeriodIndex (start = start )
133+ with pytest .raises (ValueError , match = msg ):
134+ PeriodIndex (end = end )
117135
118136 result = period_range ('2007-01' , periods = 10.5 , freq = 'M' )
119137 exp = period_range ('2007-01' , periods = 10 , freq = 'M' )
@@ -126,10 +144,15 @@ def test_constructor_fromarraylike(self):
126144 tm .assert_index_equal (PeriodIndex (idx .values ), idx )
127145 tm .assert_index_equal (PeriodIndex (list (idx .values )), idx )
128146
129- pytest .raises (ValueError , PeriodIndex , idx ._ndarray_values )
130- pytest .raises (ValueError , PeriodIndex , list (idx ._ndarray_values ))
131- pytest .raises (TypeError , PeriodIndex ,
132- data = Period ('2007' , freq = 'A' ))
147+ msg = "freq not specified and cannot be inferred"
148+ with pytest .raises (ValueError , match = msg ):
149+ PeriodIndex (idx ._ndarray_values )
150+ with pytest .raises (ValueError , match = msg ):
151+ PeriodIndex (list (idx ._ndarray_values ))
152+
153+ msg = "'Period' object is not iterable"
154+ with pytest .raises (TypeError , match = msg ):
155+ PeriodIndex (data = Period ('2007' , freq = 'A' ))
133156
134157 result = PeriodIndex (iter (idx ))
135158 tm .assert_index_equal (result , idx )
@@ -160,7 +183,9 @@ def test_constructor_datetime64arr(self):
160183 vals = np .arange (100000 , 100000 + 10000 , 100 , dtype = np .int64 )
161184 vals = vals .view (np .dtype ('M8[us]' ))
162185
163- pytest .raises (ValueError , PeriodIndex , vals , freq = 'D' )
186+ msg = r"Wrong dtype: datetime64\[us\]"
187+ with pytest .raises (ValueError , match = msg ):
188+ PeriodIndex (vals , freq = 'D' )
164189
165190 @pytest .mark .parametrize ('box' , [None , 'series' , 'index' ])
166191 def test_constructor_datetime64arr_ok (self , box ):
@@ -300,17 +325,20 @@ def test_constructor_simple_new_empty(self):
300325
301326 @pytest .mark .parametrize ('floats' , [[1.1 , 2.1 ], np .array ([1.1 , 2.1 ])])
302327 def test_constructor_floats (self , floats ):
303- with pytest .raises (TypeError ):
328+ msg = r"PeriodIndex\._simple_new does not accept floats"
329+ with pytest .raises (TypeError , match = msg ):
304330 pd .PeriodIndex ._simple_new (floats , freq = 'M' )
305331
306- with pytest .raises (TypeError ):
332+ msg = "PeriodIndex does not allow floating point in construction"
333+ with pytest .raises (TypeError , match = msg ):
307334 pd .PeriodIndex (floats , freq = 'M' )
308335
309336 def test_constructor_nat (self ):
310- pytest .raises (ValueError , period_range , start = 'NaT' ,
311- end = '2011-01-01' , freq = 'M' )
312- pytest .raises (ValueError , period_range , start = '2011-01-01' ,
313- end = 'NaT' , freq = 'M' )
337+ msg = "start and end must not be NaT"
338+ with pytest .raises (ValueError , match = msg ):
339+ period_range (start = 'NaT' , end = '2011-01-01' , freq = 'M' )
340+ with pytest .raises (ValueError , match = msg ):
341+ period_range (start = '2011-01-01' , end = 'NaT' , freq = 'M' )
314342
315343 def test_constructor_year_and_quarter (self ):
316344 year = pd .Series ([2001 , 2002 , 2003 ])
@@ -455,9 +483,12 @@ def test_constructor(self):
455483
456484 # Mixed freq should fail
457485 vals = [end_intv , Period ('2006-12-31' , 'w' )]
458- pytest .raises (ValueError , PeriodIndex , vals )
486+ msg = r"Input has different freq=W-SUN from PeriodIndex\(freq=B\)"
487+ with pytest .raises (IncompatibleFrequency , match = msg ):
488+ PeriodIndex (vals )
459489 vals = np .array (vals )
460- pytest .raises (ValueError , PeriodIndex , vals )
490+ with pytest .raises (IncompatibleFrequency , match = msg ):
491+ PeriodIndex (vals )
461492
462493 def test_constructor_error (self ):
463494 start = Period ('02-Apr-2005' , 'B' )
@@ -508,7 +539,8 @@ def setup_method(self, method):
508539 self .series = Series (period_range ('2000-01-01' , periods = 10 , freq = 'D' ))
509540
510541 def test_constructor_cant_cast_period (self ):
511- with pytest .raises (TypeError ):
542+ msg = "Cannot cast PeriodArray to dtype float64"
543+ with pytest .raises (TypeError , match = msg ):
512544 Series (period_range ('2000-01-01' , periods = 10 , freq = 'D' ),
513545 dtype = float )
514546
0 commit comments