1
1
import numpy as np
2
2
import pytest
3
3
4
+ from pandas ._libs .tslibs .period import IncompatibleFrequency
4
5
from pandas .compat import PY3 , lmap , lrange , text_type
5
6
6
7
from pandas .core .dtypes .dtypes import PeriodDtype
@@ -66,12 +67,17 @@ def test_constructor_field_arrays(self):
66
67
67
68
years = [2007 , 2007 , 2007 ]
68
69
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' ))
75
81
76
82
years = [2007 , 2007 , 2007 ]
77
83
months = [1 , 2 , 3 ]
@@ -81,8 +87,8 @@ def test_constructor_field_arrays(self):
81
87
82
88
def test_constructor_U (self ):
83
89
# 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' )
86
92
87
93
def test_constructor_nano (self ):
88
94
idx = period_range (start = Period (ordinal = 1 , freq = 'N' ),
@@ -103,17 +109,29 @@ def test_constructor_arrays_negative_year(self):
103
109
tm .assert_index_equal (pindex .quarter , pd .Index (quarters ))
104
110
105
111
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' )
108
116
109
117
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' )
111
121
112
122
start = Period ('2007' , freq = 'A-JUN' )
113
123
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 )
117
135
118
136
result = period_range ('2007-01' , periods = 10.5 , freq = 'M' )
119
137
exp = period_range ('2007-01' , periods = 10 , freq = 'M' )
@@ -126,10 +144,15 @@ def test_constructor_fromarraylike(self):
126
144
tm .assert_index_equal (PeriodIndex (idx .values ), idx )
127
145
tm .assert_index_equal (PeriodIndex (list (idx .values )), idx )
128
146
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' ))
133
156
134
157
result = PeriodIndex (iter (idx ))
135
158
tm .assert_index_equal (result , idx )
@@ -160,7 +183,9 @@ def test_constructor_datetime64arr(self):
160
183
vals = np .arange (100000 , 100000 + 10000 , 100 , dtype = np .int64 )
161
184
vals = vals .view (np .dtype ('M8[us]' ))
162
185
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' )
164
189
165
190
@pytest .mark .parametrize ('box' , [None , 'series' , 'index' ])
166
191
def test_constructor_datetime64arr_ok (self , box ):
@@ -300,17 +325,20 @@ def test_constructor_simple_new_empty(self):
300
325
301
326
@pytest .mark .parametrize ('floats' , [[1.1 , 2.1 ], np .array ([1.1 , 2.1 ])])
302
327
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 ):
304
330
pd .PeriodIndex ._simple_new (floats , freq = 'M' )
305
331
306
- with pytest .raises (TypeError ):
332
+ msg = "PeriodIndex does not allow floating point in construction"
333
+ with pytest .raises (TypeError , match = msg ):
307
334
pd .PeriodIndex (floats , freq = 'M' )
308
335
309
336
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' )
314
342
315
343
def test_constructor_year_and_quarter (self ):
316
344
year = pd .Series ([2001 , 2002 , 2003 ])
@@ -455,9 +483,12 @@ def test_constructor(self):
455
483
456
484
# Mixed freq should fail
457
485
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 )
459
489
vals = np .array (vals )
460
- pytest .raises (ValueError , PeriodIndex , vals )
490
+ with pytest .raises (IncompatibleFrequency , match = msg ):
491
+ PeriodIndex (vals )
461
492
462
493
def test_constructor_error (self ):
463
494
start = Period ('02-Apr-2005' , 'B' )
@@ -508,7 +539,8 @@ def setup_method(self, method):
508
539
self .series = Series (period_range ('2000-01-01' , periods = 10 , freq = 'D' ))
509
540
510
541
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 ):
512
544
Series (period_range ('2000-01-01' , periods = 10 , freq = 'D' ),
513
545
dtype = float )
514
546
0 commit comments