@@ -479,7 +479,12 @@ def test_to_datetime_parse_timezone_malformed(self, offset):
479
479
fmt = "%Y-%m-%d %H:%M:%S %z"
480
480
date = "2010-01-01 12:00:00 " + offset
481
481
482
- msg = "does not match format|unconverted data remains"
482
+ msg = "|" .join (
483
+ [
484
+ r'^time data ".*" at position 0 doesn\'t match format ".*"$' ,
485
+ r'^unconverted data remains at position 0: ".*"$' ,
486
+ ]
487
+ )
483
488
with pytest .raises (ValueError , match = msg ):
484
489
to_datetime ([date ], format = fmt )
485
490
@@ -1093,7 +1098,7 @@ def test_datetime_bool_arrays_mixed(self, cache):
1093
1098
to_datetime ([False , datetime .today ()], cache = cache )
1094
1099
with pytest .raises (
1095
1100
ValueError ,
1096
- match = r" ^time data ' True' does not match format ' %Y%m%d' \(match\)$" ,
1101
+ match = r' ^time data " True" at position 1 doesn\'t match format " %Y%m%d"$' ,
1097
1102
):
1098
1103
to_datetime (["20130101" , True ], cache = cache )
1099
1104
tm .assert_index_equal (
@@ -1132,11 +1137,13 @@ def test_datetime_invalid_scalar(self, value, format, warning):
1132
1137
res = to_datetime (value , errors = "coerce" , format = format )
1133
1138
assert res is NaT
1134
1139
1135
- msg = (
1136
- "does not match format|"
1137
- "unconverted data remains:|"
1138
- "second must be in 0..59|"
1139
- f"Given date string { value } not likely a datetime"
1140
+ msg = "|" .join (
1141
+ [
1142
+ r'^time data "a" at position 0 doesn\'t match format "%H:%M:%S"$' ,
1143
+ r'^Given date string "a" not likely a datetime present at position 0$' ,
1144
+ r'^unconverted data remains at position 0: "9"$' ,
1145
+ r"^second must be in 0..59: 00:01:99 present at position 0$" ,
1146
+ ]
1140
1147
)
1141
1148
with pytest .raises (ValueError , match = msg ):
1142
1149
with tm .assert_produces_warning (warning , match = "Could not infer format" ):
@@ -1157,7 +1164,7 @@ def test_datetime_outofbounds_scalar(self, value, format, warning):
1157
1164
assert res is NaT
1158
1165
1159
1166
if format is not None :
1160
- msg = "does not match format|Out of bounds .* present at position 0"
1167
+ msg = r'^time data ".*" at position 0 doesn\'t match format ".*"$'
1161
1168
with pytest .raises (ValueError , match = msg ):
1162
1169
to_datetime (value , errors = "raise" , format = format )
1163
1170
else :
@@ -1181,11 +1188,13 @@ def test_datetime_invalid_index(self, values, format, warning):
1181
1188
res = to_datetime (values , errors = "coerce" , format = format )
1182
1189
tm .assert_index_equal (res , DatetimeIndex ([NaT ] * len (values )))
1183
1190
1184
- msg = (
1185
- "does not match format|"
1186
- "unconverted data remains:|"
1187
- f"Given date string { values [0 ]} not likely a datetime|"
1188
- "second must be in 0..59"
1191
+ msg = "|" .join (
1192
+ [
1193
+ r'^Given date string "a" not likely a datetime present at position 0$' ,
1194
+ r'^time data "a" at position 0 doesn\'t match format "%H:%M:%S"$' ,
1195
+ r'^unconverted data remains at position 0: "9"$' ,
1196
+ r"^second must be in 0..59: 00:01:99 present at position 0$" ,
1197
+ ]
1189
1198
)
1190
1199
with pytest .raises (ValueError , match = msg ):
1191
1200
with tm .assert_produces_warning (warning , match = "Could not infer format" ):
@@ -1805,8 +1814,8 @@ def test_dataframe_coerce(self, cache):
1805
1814
df2 = DataFrame ({"year" : [2015 , 2016 ], "month" : [2 , 20 ], "day" : [4 , 5 ]})
1806
1815
1807
1816
msg = (
1808
- " cannot assemble the datetimes: time data .+ does not "
1809
- r" match format ' %Y%m%d' \(match\)"
1817
+ r'^ cannot assemble the datetimes: time data ".+" at position 1 doesn\'t '
1818
+ r' match format " %Y%m%d"$'
1810
1819
)
1811
1820
with pytest .raises (ValueError , match = msg ):
1812
1821
to_datetime (df2 , cache = cache )
@@ -1882,7 +1891,10 @@ def test_dataframe_mixed(self, cache):
1882
1891
def test_dataframe_float (self , cache ):
1883
1892
# float
1884
1893
df = DataFrame ({"year" : [2000 , 2001 ], "month" : [1.5 , 1 ], "day" : [1 , 1 ]})
1885
- msg = "cannot assemble the datetimes: unconverted data remains: 1"
1894
+ msg = (
1895
+ r"^cannot assemble the datetimes: unconverted data remains at position "
1896
+ r'0: "1"$'
1897
+ )
1886
1898
with pytest .raises (ValueError , match = msg ):
1887
1899
to_datetime (df , cache = cache )
1888
1900
@@ -2072,7 +2084,7 @@ def test_to_datetime_on_datetime64_series(self, cache):
2072
2084
def test_to_datetime_with_space_in_series (self , cache ):
2073
2085
# GH 6428
2074
2086
ser = Series (["10/18/2006" , "10/18/2008" , " " ])
2075
- msg = r" ^time data ' ' does not match format ' %m/%d/%Y' \(match\)$"
2087
+ msg = r' ^time data " " at position 2 doesn\'t match format " %m/%d/%Y"$'
2076
2088
with pytest .raises (ValueError , match = msg ):
2077
2089
to_datetime (ser , errors = "raise" , cache = cache )
2078
2090
result_coerce = to_datetime (ser , errors = "coerce" , cache = cache )
@@ -2342,7 +2354,10 @@ def test_dayfirst_warnings_invalid_input(self):
2342
2354
2343
2355
with pytest .raises (
2344
2356
ValueError ,
2345
- match = r"time data '03/30/2011' does not match format '%d/%m/%Y' \(match\)$" ,
2357
+ match = (
2358
+ r'^time data "03/30/2011" at position 1 doesn\'t match format '
2359
+ r'"%d/%m/%Y"$'
2360
+ ),
2346
2361
):
2347
2362
to_datetime (arr , dayfirst = True )
2348
2363
@@ -2410,7 +2425,11 @@ def test_to_datetime_infer_datetime_format_consistent_format(
2410
2425
def test_to_datetime_inconsistent_format (self , cache ):
2411
2426
data = ["01/01/2011 00:00:00" , "01-02-2011 00:00:00" , "2011-01-03T00:00:00" ]
2412
2427
ser = Series (np .array (data ))
2413
- with pytest .raises (ValueError , match = "does not match format" ):
2428
+ msg = (
2429
+ r'^time data "01-02-2011 00:00:00" at position 1 doesn\'t match format '
2430
+ r'"%m/%d/%Y %H:%M:%S"$'
2431
+ )
2432
+ with pytest .raises (ValueError , match = msg ):
2414
2433
to_datetime (ser , cache = cache )
2415
2434
2416
2435
def test_to_datetime_consistent_format (self , cache ):
@@ -2923,17 +2942,22 @@ def test_incorrect_value_exception(self):
2923
2942
to_datetime (["today" , "yesterday" ])
2924
2943
2925
2944
@pytest .mark .parametrize (
2926
- "format, warning" , [(None , UserWarning ), ("%Y-%m-%d %H:%M:%S" , None )]
2945
+ "format, warning" ,
2946
+ [
2947
+ (None , UserWarning ),
2948
+ ("%Y-%m-%d %H:%M:%S" , None ),
2949
+ ("%Y-%d-%m %H:%M:%S" , None ),
2950
+ ],
2927
2951
)
2928
2952
def test_to_datetime_out_of_bounds_with_format_arg (self , format , warning ):
2929
2953
# see gh-23830
2930
2954
msg = (
2931
- " Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 "
2932
- "present at position 0"
2955
+ r"^ Out of bounds nanosecond timestamp: 2417-10-10 00:00:00 "
2956
+ r "present at position 0$ "
2933
2957
)
2934
2958
with pytest .raises (OutOfBoundsDatetime , match = msg ):
2935
2959
with tm .assert_produces_warning (warning , match = "Could not infer format" ):
2936
- to_datetime ("2417-10-27 00:00:00" , format = format )
2960
+ to_datetime ("2417-10-10 00:00:00" , format = format )
2937
2961
2938
2962
@pytest .mark .parametrize (
2939
2963
"arg, origin, expected_str" ,
0 commit comments