@@ -79,7 +79,8 @@ def test_construction(self):
79
79
with pytest .raises (ValueError , match = msg ):
80
80
Period (ordinal = 200701 )
81
81
82
- with pytest .raises (ValueError , match = "Invalid frequency: X" ):
82
+ msg = "Invalid frequency: X"
83
+ with pytest .raises (ValueError , match = msg ):
83
84
Period ("2007-1-1" , freq = "X" )
84
85
85
86
def test_construction_bday (self ):
@@ -235,26 +236,34 @@ def test_period_constructor_offsets(self):
235
236
assert i1 == expected
236
237
237
238
def test_invalid_arguments (self ):
238
- with pytest .raises (ValueError ):
239
+ msg = "Must supply freq for datetime value"
240
+ with pytest .raises (ValueError , match = msg ):
239
241
Period (datetime .now ())
240
- with pytest .raises (ValueError ):
242
+ with pytest .raises (ValueError , match = msg ):
241
243
Period (datetime .now ().date ())
242
244
243
- with pytest .raises (ValueError ):
245
+ msg = "Value must be Period, string, integer, or datetime"
246
+ with pytest .raises (ValueError , match = msg ):
244
247
Period (1.6 , freq = "D" )
245
- with pytest .raises (ValueError ):
248
+ msg = "Ordinal must be an integer"
249
+ with pytest .raises (ValueError , match = msg ):
246
250
Period (ordinal = 1.6 , freq = "D" )
247
- with pytest .raises (ValueError ):
251
+ msg = "Only value or ordinal but not both should be given but not both"
252
+ with pytest .raises (ValueError , match = msg ):
248
253
Period (ordinal = 2 , value = 1 , freq = "D" )
249
254
250
- with pytest .raises (ValueError ):
255
+ msg = "If value is None, freq cannot be None"
256
+ with pytest .raises (ValueError , match = msg ):
251
257
Period (month = 1 )
252
258
253
- with pytest .raises (ValueError ):
259
+ msg = "Given date string not likely a datetime"
260
+ with pytest .raises (ValueError , match = msg ):
254
261
Period ("-2000" , "A" )
255
- with pytest .raises (DateParseError ):
262
+ msg = "day is out of range for month"
263
+ with pytest .raises (DateParseError , match = msg ):
256
264
Period ("0" , "A" )
257
- with pytest .raises (DateParseError ):
265
+ msg = "Unknown datetime string format, unable to parse"
266
+ with pytest .raises (DateParseError , match = msg ):
258
267
Period ("1/1/-2000" , "A" )
259
268
260
269
def test_constructor_corner (self ):
@@ -1030,7 +1039,8 @@ def test_sub_delta(self):
1030
1039
result = left - right
1031
1040
assert result == 4 * right .freq
1032
1041
1033
- with pytest .raises (IncompatibleFrequency ):
1042
+ msg = r"Input has different freq=M from Period\(freq=A-DEC\)"
1043
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1034
1044
left - Period ("2007-01" , freq = "M" )
1035
1045
1036
1046
def test_add_integer (self ):
@@ -1072,10 +1082,14 @@ def test_add_timestamp_raises(self, rbox, lbox):
1072
1082
1073
1083
# We may get a different message depending on which class raises
1074
1084
# the error.
1075
- msg = (
1076
- r"cannot add|unsupported operand|"
1077
- r"can only operate on a|incompatible type|"
1078
- r"ufunc add cannot use operands"
1085
+ msg = "|" .join (
1086
+ [
1087
+ "cannot add" ,
1088
+ "unsupported operand" ,
1089
+ "can only operate on a" ,
1090
+ "incompatible type" ,
1091
+ "ufunc add cannot use operands" ,
1092
+ ]
1079
1093
)
1080
1094
with pytest .raises (TypeError , match = msg ):
1081
1095
lbox (ts ) + rbox (per )
@@ -1148,14 +1162,22 @@ def test_add_offset(self):
1148
1162
np .timedelta64 (365 , "D" ),
1149
1163
timedelta (365 ),
1150
1164
]:
1151
- with pytest .raises (IncompatibleFrequency ):
1165
+ msg = "Input has different freq|Input cannot be converted to Period"
1166
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1152
1167
p + o
1153
1168
1154
1169
if isinstance (o , np .timedelta64 ):
1155
- with pytest .raises (TypeError ):
1170
+ msg = "cannot use operands with types"
1171
+ with pytest .raises (TypeError , match = msg ):
1156
1172
o + p
1157
1173
else :
1158
- with pytest .raises (IncompatibleFrequency ):
1174
+ msg = "|" .join (
1175
+ [
1176
+ "Input has different freq" ,
1177
+ "Input cannot be converted to Period" ,
1178
+ ]
1179
+ )
1180
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1159
1181
o + p
1160
1182
1161
1183
for freq in ["M" , "2M" , "3M" ]:
@@ -1175,14 +1197,22 @@ def test_add_offset(self):
1175
1197
np .timedelta64 (365 , "D" ),
1176
1198
timedelta (365 ),
1177
1199
]:
1178
- with pytest .raises (IncompatibleFrequency ):
1200
+ msg = "Input has different freq|Input cannot be converted to Period"
1201
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1179
1202
p + o
1180
1203
1181
1204
if isinstance (o , np .timedelta64 ):
1182
- with pytest .raises (TypeError ):
1205
+ msg = "cannot use operands with types"
1206
+ with pytest .raises (TypeError , match = msg ):
1183
1207
o + p
1184
1208
else :
1185
- with pytest .raises (IncompatibleFrequency ):
1209
+ msg = "|" .join (
1210
+ [
1211
+ "Input has different freq" ,
1212
+ "Input cannot be converted to Period" ,
1213
+ ]
1214
+ )
1215
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1186
1216
o + p
1187
1217
1188
1218
# freq is Tick
@@ -1199,12 +1229,13 @@ def test_add_offset(self):
1199
1229
1200
1230
exp = Period ("2011-04-03" , freq = freq )
1201
1231
assert p + np .timedelta64 (2 , "D" ) == exp
1202
- with pytest .raises (TypeError ):
1232
+ msg = "cannot use operands with types"
1233
+ with pytest .raises (TypeError , match = msg ):
1203
1234
np .timedelta64 (2 , "D" ) + p
1204
1235
1205
1236
exp = Period ("2011-04-02" , freq = freq )
1206
1237
assert p + np .timedelta64 (3600 * 24 , "s" ) == exp
1207
- with pytest .raises (TypeError ):
1238
+ with pytest .raises (TypeError , match = msg ):
1208
1239
np .timedelta64 (3600 * 24 , "s" ) + p
1209
1240
1210
1241
exp = Period ("2011-03-30" , freq = freq )
@@ -1222,14 +1253,22 @@ def test_add_offset(self):
1222
1253
np .timedelta64 (4 , "h" ),
1223
1254
timedelta (hours = 23 ),
1224
1255
]:
1225
- with pytest .raises (IncompatibleFrequency ):
1256
+ msg = "Input has different freq|Input cannot be converted to Period"
1257
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1226
1258
p + o
1227
1259
1228
1260
if isinstance (o , np .timedelta64 ):
1229
- with pytest .raises (TypeError ):
1261
+ msg = "cannot use operands with types"
1262
+ with pytest .raises (TypeError , match = msg ):
1230
1263
o + p
1231
1264
else :
1232
- with pytest .raises (IncompatibleFrequency ):
1265
+ msg = "|" .join (
1266
+ [
1267
+ "Input has different freq" ,
1268
+ "Input cannot be converted to Period" ,
1269
+ ]
1270
+ )
1271
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1233
1272
o + p
1234
1273
1235
1274
for freq in ["H" , "2H" , "3H" ]:
@@ -1243,14 +1282,15 @@ def test_add_offset(self):
1243
1282
assert p + offsets .Hour (3 ) == exp
1244
1283
assert offsets .Hour (3 ) + p == exp
1245
1284
1285
+ msg = "cannot use operands with types"
1246
1286
exp = Period ("2011-04-01 12:00" , freq = freq )
1247
1287
assert p + np .timedelta64 (3 , "h" ) == exp
1248
- with pytest .raises (TypeError ):
1288
+ with pytest .raises (TypeError , match = msg ):
1249
1289
np .timedelta64 (3 , "h" ) + p
1250
1290
1251
1291
exp = Period ("2011-04-01 10:00" , freq = freq )
1252
1292
assert p + np .timedelta64 (3600 , "s" ) == exp
1253
- with pytest .raises (TypeError ):
1293
+ with pytest .raises (TypeError , match = msg ):
1254
1294
np .timedelta64 (3600 , "s" ) + p
1255
1295
1256
1296
exp = Period ("2011-04-01 11:00" , freq = freq )
@@ -1268,18 +1308,27 @@ def test_add_offset(self):
1268
1308
np .timedelta64 (3200 , "s" ),
1269
1309
timedelta (hours = 23 , minutes = 30 ),
1270
1310
]:
1271
- with pytest .raises (IncompatibleFrequency ):
1311
+ msg = "Input has different freq|Input cannot be converted to Period"
1312
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1272
1313
p + o
1273
1314
1274
1315
if isinstance (o , np .timedelta64 ):
1275
- with pytest .raises (TypeError ):
1316
+ msg = "cannot use operands with types"
1317
+ with pytest .raises (TypeError , match = msg ):
1276
1318
o + p
1277
1319
else :
1278
- with pytest .raises (IncompatibleFrequency ):
1320
+ msg = "|" .join (
1321
+ [
1322
+ "Input has different freq" ,
1323
+ "Input cannot be converted to Period" ,
1324
+ ]
1325
+ )
1326
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1279
1327
o + p
1280
1328
1281
1329
def test_sub_offset (self ):
1282
1330
# freq is DateOffset
1331
+ msg = "Input has different freq|Input cannot be converted to Period"
1283
1332
for freq in ["A" , "2A" , "3A" ]:
1284
1333
p = Period ("2011" , freq = freq )
1285
1334
assert p - offsets .YearEnd (2 ) == Period ("2009" , freq = freq )
@@ -1291,7 +1340,7 @@ def test_sub_offset(self):
1291
1340
np .timedelta64 (365 , "D" ),
1292
1341
timedelta (365 ),
1293
1342
]:
1294
- with pytest .raises (IncompatibleFrequency ):
1343
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1295
1344
p - o
1296
1345
1297
1346
for freq in ["M" , "2M" , "3M" ]:
@@ -1306,7 +1355,7 @@ def test_sub_offset(self):
1306
1355
np .timedelta64 (365 , "D" ),
1307
1356
timedelta (365 ),
1308
1357
]:
1309
- with pytest .raises (IncompatibleFrequency ):
1358
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1310
1359
p - o
1311
1360
1312
1361
# freq is Tick
@@ -1326,7 +1375,7 @@ def test_sub_offset(self):
1326
1375
np .timedelta64 (4 , "h" ),
1327
1376
timedelta (hours = 23 ),
1328
1377
]:
1329
- with pytest .raises (IncompatibleFrequency ):
1378
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1330
1379
p - o
1331
1380
1332
1381
for freq in ["H" , "2H" , "3H" ]:
@@ -1349,7 +1398,7 @@ def test_sub_offset(self):
1349
1398
np .timedelta64 (3200 , "s" ),
1350
1399
timedelta (hours = 23 , minutes = 30 ),
1351
1400
]:
1352
- with pytest .raises (IncompatibleFrequency ):
1401
+ with pytest .raises (IncompatibleFrequency , match = msg ):
1353
1402
p - o
1354
1403
1355
1404
@pytest .mark .parametrize ("freq" , ["M" , "2M" , "3M" ])
@@ -1377,12 +1426,14 @@ def test_period_ops_offset(self):
1377
1426
1378
1427
def test_period_immutable ():
1379
1428
# see gh-17116
1429
+ msg = "not writable"
1430
+
1380
1431
per = Period ("2014Q1" )
1381
- with pytest .raises (AttributeError ):
1432
+ with pytest .raises (AttributeError , match = msg ):
1382
1433
per .ordinal = 14
1383
1434
1384
1435
freq = per .freq
1385
- with pytest .raises (AttributeError ):
1436
+ with pytest .raises (AttributeError , match = msg ):
1386
1437
per .freq = 2 * freq
1387
1438
1388
1439
0 commit comments