@@ -349,7 +349,9 @@ def test_to_datetime_parse_tzname_or_tzoffset_different_tz_to_utc(self):
349
349
def test_to_datetime_parse_timezone_malformed (self , offset ):
350
350
fmt = "%Y-%m-%d %H:%M:%S %z"
351
351
date = "2010-01-01 12:00:00 " + offset
352
- with pytest .raises (ValueError ):
352
+
353
+ msg = "does not match format|unconverted data remains"
354
+ with pytest .raises (ValueError , match = msg ):
353
355
pd .to_datetime ([date ], format = fmt )
354
356
355
357
def test_to_datetime_parse_timezone_keeps_name (self ):
@@ -784,17 +786,19 @@ def test_to_datetime_tz_psycopg2(self, cache):
784
786
@pytest .mark .parametrize ("cache" , [True , False ])
785
787
def test_datetime_bool (self , cache ):
786
788
# GH13176
787
- with pytest .raises (TypeError ):
789
+ msg = r"dtype bool cannot be converted to datetime64\[ns\]"
790
+ with pytest .raises (TypeError , match = msg ):
788
791
to_datetime (False )
789
792
assert to_datetime (False , errors = "coerce" , cache = cache ) is NaT
790
793
assert to_datetime (False , errors = "ignore" , cache = cache ) is False
791
- with pytest .raises (TypeError ):
794
+ with pytest .raises (TypeError , match = msg ):
792
795
to_datetime (True )
793
796
assert to_datetime (True , errors = "coerce" , cache = cache ) is NaT
794
797
assert to_datetime (True , errors = "ignore" , cache = cache ) is True
795
- with pytest .raises (TypeError ):
798
+ msg = f"{ type (cache )} is not convertible to datetime"
799
+ with pytest .raises (TypeError , match = msg ):
796
800
to_datetime ([False , datetime .today ()], cache = cache )
797
- with pytest .raises (TypeError ):
801
+ with pytest .raises (TypeError , match = msg ):
798
802
to_datetime (["20130101" , True ], cache = cache )
799
803
tm .assert_index_equal (
800
804
to_datetime ([0 , False , NaT , 0.0 ], errors = "coerce" , cache = cache ),
@@ -805,10 +809,10 @@ def test_datetime_bool(self, cache):
805
809
806
810
def test_datetime_invalid_datatype (self ):
807
811
# GH13176
808
-
809
- with pytest .raises (TypeError ):
812
+ msg = "is not convertible to datetime"
813
+ with pytest .raises (TypeError , match = msg ):
810
814
pd .to_datetime (bool )
811
- with pytest .raises (TypeError ):
815
+ with pytest .raises (TypeError , match = msg ):
812
816
pd .to_datetime (pd .to_datetime )
813
817
814
818
@pytest .mark .parametrize ("value" , ["a" , "00:01:99" ])
@@ -826,7 +830,12 @@ def test_datetime_invalid_scalar(self, value, format, infer):
826
830
)
827
831
assert res is pd .NaT
828
832
829
- with pytest .raises (ValueError ):
833
+ msg = (
834
+ "is a bad directive in format|"
835
+ "second must be in 0..59: 00:01:99|"
836
+ "Given date string not likely a datetime"
837
+ )
838
+ with pytest .raises (ValueError , match = msg ):
830
839
pd .to_datetime (
831
840
value , errors = "raise" , format = format , infer_datetime_format = infer
832
841
)
@@ -847,12 +856,14 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
847
856
assert res is pd .NaT
848
857
849
858
if format is not None :
850
- with pytest .raises (ValueError ):
859
+ msg = "is a bad directive in format|Out of bounds nanosecond timestamp"
860
+ with pytest .raises (ValueError , match = msg ):
851
861
pd .to_datetime (
852
862
value , errors = "raise" , format = format , infer_datetime_format = infer
853
863
)
854
864
else :
855
- with pytest .raises (OutOfBoundsDatetime ):
865
+ msg = "Out of bounds nanosecond timestamp"
866
+ with pytest .raises (OutOfBoundsDatetime , match = msg ):
856
867
pd .to_datetime (
857
868
value , errors = "raise" , format = format , infer_datetime_format = infer
858
869
)
@@ -872,7 +883,12 @@ def test_datetime_invalid_index(self, values, format, infer):
872
883
)
873
884
tm .assert_index_equal (res , pd .DatetimeIndex ([pd .NaT ] * len (values )))
874
885
875
- with pytest .raises (ValueError ):
886
+ msg = (
887
+ "is a bad directive in format|"
888
+ "Given date string not likely a datetime|"
889
+ "second must be in 0..59: 00:01:99"
890
+ )
891
+ with pytest .raises (ValueError , match = msg ):
876
892
pd .to_datetime (
877
893
values , errors = "raise" , format = format , infer_datetime_format = infer
878
894
)
@@ -1070,7 +1086,8 @@ def test_timestamp_utc_true(self, ts, expected):
1070
1086
@pytest .mark .parametrize ("dt_str" , ["00010101" , "13000101" , "30000101" , "99990101" ])
1071
1087
def test_to_datetime_with_format_out_of_bounds (self , dt_str ):
1072
1088
# GH 9107
1073
- with pytest .raises (OutOfBoundsDatetime ):
1089
+ msg = "Out of bounds nanosecond timestamp"
1090
+ with pytest .raises (OutOfBoundsDatetime , match = msg ):
1074
1091
pd .to_datetime (dt_str , format = "%Y%m%d" )
1075
1092
1076
1093
def test_to_datetime_utc (self ):
@@ -1096,8 +1113,8 @@ class TestToDatetimeUnit:
1096
1113
def test_unit (self , cache ):
1097
1114
# GH 11758
1098
1115
# test proper behavior with errors
1099
-
1100
- with pytest .raises (ValueError ):
1116
+ msg = "cannot specify both format and unit"
1117
+ with pytest .raises (ValueError , match = msg ):
1101
1118
to_datetime ([1 ], unit = "D" , format = "%Y%m%d" , cache = cache )
1102
1119
1103
1120
values = [11111111 , 1 , 1.0 , iNaT , NaT , np .nan , "NaT" , "" ]
@@ -1123,7 +1140,8 @@ def test_unit(self, cache):
1123
1140
)
1124
1141
tm .assert_index_equal (result , expected )
1125
1142
1126
- with pytest .raises (tslib .OutOfBoundsDatetime ):
1143
+ msg = "cannot convert input 11111111 with the unit 'D'"
1144
+ with pytest .raises (tslib .OutOfBoundsDatetime , match = msg ):
1127
1145
to_datetime (values , unit = "D" , errors = "raise" , cache = cache )
1128
1146
1129
1147
values = [1420043460000 , iNaT , NaT , np .nan , "NaT" ]
@@ -1136,7 +1154,8 @@ def test_unit(self, cache):
1136
1154
expected = DatetimeIndex (["NaT" , "NaT" , "NaT" , "NaT" , "NaT" ])
1137
1155
tm .assert_index_equal (result , expected )
1138
1156
1139
- with pytest .raises (tslib .OutOfBoundsDatetime ):
1157
+ msg = "cannot convert input 1420043460000 with the unit 's'"
1158
+ with pytest .raises (tslib .OutOfBoundsDatetime , match = msg ):
1140
1159
to_datetime (values , errors = "raise" , unit = "s" , cache = cache )
1141
1160
1142
1161
# if we have a string, then we raise a ValueError
@@ -1204,15 +1223,16 @@ def test_unit_mixed(self, cache):
1204
1223
result = pd .to_datetime (arr , errors = "coerce" , cache = cache )
1205
1224
tm .assert_index_equal (result , expected )
1206
1225
1207
- with pytest .raises (ValueError ):
1226
+ msg = "mixed datetimes and integers in passed array"
1227
+ with pytest .raises (ValueError , match = msg ):
1208
1228
pd .to_datetime (arr , errors = "raise" , cache = cache )
1209
1229
1210
1230
expected = DatetimeIndex (["NaT" , "NaT" , "2013-01-01" ])
1211
1231
arr = [1.434692e18 , 1.432766e18 , pd .Timestamp ("20130101" )]
1212
1232
result = pd .to_datetime (arr , errors = "coerce" , cache = cache )
1213
1233
tm .assert_index_equal (result , expected )
1214
1234
1215
- with pytest .raises (ValueError ):
1235
+ with pytest .raises (ValueError , match = msg ):
1216
1236
pd .to_datetime (arr , errors = "raise" , cache = cache )
1217
1237
1218
1238
@pytest .mark .parametrize ("cache" , [True , False ])
@@ -1392,7 +1412,8 @@ def test_dataframe_dtypes(self, cache):
1392
1412
1393
1413
# float
1394
1414
df = DataFrame ({"year" : [2000 , 2001 ], "month" : [1.5 , 1 ], "day" : [1 , 1 ]})
1395
- with pytest .raises (ValueError ):
1415
+ msg = "cannot assemble the datetimes: unconverted data remains: 1"
1416
+ with pytest .raises (ValueError , match = msg ):
1396
1417
to_datetime (df , cache = cache )
1397
1418
1398
1419
def test_dataframe_utc_true (self ):
@@ -1500,7 +1521,8 @@ def test_to_datetime_barely_out_of_bounds(self):
1500
1521
# in an in-bounds datetime
1501
1522
arr = np .array (["2262-04-11 23:47:16.854775808" ], dtype = object )
1502
1523
1503
- with pytest .raises (OutOfBoundsDatetime ):
1524
+ msg = "Out of bounds nanosecond timestamp"
1525
+ with pytest .raises (OutOfBoundsDatetime , match = msg ):
1504
1526
to_datetime (arr )
1505
1527
1506
1528
@pytest .mark .parametrize ("cache" , [True , False ])
@@ -1638,7 +1660,8 @@ def test_to_datetime_overflow(self):
1638
1660
# gh-17637
1639
1661
# we are overflowing Timedelta range here
1640
1662
1641
- with pytest .raises (OverflowError ):
1663
+ msg = "Python int too large to convert to C long"
1664
+ with pytest .raises (OverflowError , match = msg ):
1642
1665
date_range (start = "1/1/1700" , freq = "B" , periods = 100000 )
1643
1666
1644
1667
@pytest .mark .parametrize ("cache" , [True , False ])
@@ -2265,23 +2288,26 @@ def test_julian_round_trip(self):
2265
2288
assert result .to_julian_date () == 2456658
2266
2289
2267
2290
# out-of-bounds
2268
- with pytest .raises (ValueError ):
2291
+ msg = "1 is Out of Bounds for origin='julian'"
2292
+ with pytest .raises (ValueError , match = msg ):
2269
2293
pd .to_datetime (1 , origin = "julian" , unit = "D" )
2270
2294
2271
2295
def test_invalid_unit (self , units , julian_dates ):
2272
2296
2273
2297
# checking for invalid combination of origin='julian' and unit != D
2274
2298
if units != "D" :
2275
- with pytest .raises (ValueError ):
2299
+ msg = "unit must be 'D' for origin='julian'"
2300
+ with pytest .raises (ValueError , match = msg ):
2276
2301
pd .to_datetime (julian_dates , unit = units , origin = "julian" )
2277
2302
2278
2303
def test_invalid_origin (self ):
2279
2304
2280
2305
# need to have a numeric specified
2281
- with pytest .raises (ValueError ):
2306
+ msg = "it must be numeric with a unit specified"
2307
+ with pytest .raises (ValueError , match = msg ):
2282
2308
pd .to_datetime ("2005-01-01" , origin = "1960-01-01" )
2283
2309
2284
- with pytest .raises (ValueError ):
2310
+ with pytest .raises (ValueError , match = msg ):
2285
2311
pd .to_datetime ("2005-01-01" , origin = "1960-01-01" , unit = "D" )
2286
2312
2287
2313
def test_epoch (self , units , epochs , epoch_1960 , units_from_epochs ):
@@ -2304,12 +2330,13 @@ def test_epoch(self, units, epochs, epoch_1960, units_from_epochs):
2304
2330
)
2305
2331
def test_invalid_origins (self , origin , exc , units , units_from_epochs ):
2306
2332
2307
- with pytest .raises (exc ):
2333
+ msg = f"origin { origin } (is Out of Bounds|cannot be converted to a Timestamp)"
2334
+ with pytest .raises (exc , match = msg ):
2308
2335
pd .to_datetime (units_from_epochs , unit = units , origin = origin )
2309
2336
2310
2337
def test_invalid_origins_tzinfo (self ):
2311
2338
# GH16842
2312
- with pytest .raises (ValueError ):
2339
+ with pytest .raises (ValueError , match = "must be tz-naive" ):
2313
2340
pd .to_datetime (1 , unit = "D" , origin = datetime (2000 , 1 , 1 , tzinfo = pytz .utc ))
2314
2341
2315
2342
@pytest .mark .parametrize ("format" , [None , "%Y-%m-%d %H:%M:%S" ])
0 commit comments