@@ -480,8 +480,8 @@ def test_to_datetime_parse_timezone_malformed(self, offset):
480480
481481 msg = "|" .join (
482482 [
483- r'^time data ".*" at position 0 doesn\'t match format ".*"$' ,
484- r'^unconverted data remains at position 0 : ".*"$' ,
483+ r'^time data ".*" doesn\'t match format ".*", at position 0 $' ,
484+ r'^unconverted data remains: ".*", at position 0 $' ,
485485 ]
486486 )
487487 with pytest .raises (ValueError , match = msg ):
@@ -858,7 +858,7 @@ def test_to_datetime_dt64s_and_str(self, arg, format):
858858 "dt" , [np .datetime64 ("1000-01-01" ), np .datetime64 ("5000-01-02" )]
859859 )
860860 def test_to_datetime_dt64s_out_of_bounds (self , cache , dt ):
861- msg = "Out of bounds .* present at position 0"
861+ msg = "^ Out of bounds nanosecond timestamp: .*, at position 0$ "
862862 with pytest .raises (OutOfBoundsDatetime , match = msg ):
863863 to_datetime (dt , errors = "raise" )
864864
@@ -1097,7 +1097,7 @@ def test_datetime_bool_arrays_mixed(self, cache):
10971097 to_datetime ([False , datetime .today ()], cache = cache )
10981098 with pytest .raises (
10991099 ValueError ,
1100- match = r'^time data "True" at position 1 doesn\'t match format "%Y%m%d"$' ,
1100+ match = r'^time data "True" doesn\'t match format "%Y%m%d", at position 1 $' ,
11011101 ):
11021102 to_datetime (["20130101" , True ], cache = cache )
11031103 tm .assert_index_equal (
@@ -1138,10 +1138,10 @@ def test_datetime_invalid_scalar(self, value, format, warning):
11381138
11391139 msg = "|" .join (
11401140 [
1141- r'^time data "a" at position 0 doesn\'t match format "%H:%M:%S"$' ,
1142- r'^Given date string "a" not likely a datetime present at position 0$' ,
1143- r'^unconverted data remains at position 0: "9" $' ,
1144- r"^second must be in 0..59: 00:01:99 present at position 0$" ,
1141+ r'^time data "a" doesn\'t match format "%H:%M:%S", at position 0 $' ,
1142+ r'^Given date string "a" not likely a datetime, at position 0$' ,
1143+ r'^unconverted data remains: "9", at position 0$' ,
1144+ r"^second must be in 0..59: 00:01:99, at position 0$" ,
11451145 ]
11461146 )
11471147 with pytest .raises (ValueError , match = msg ):
@@ -1163,11 +1163,11 @@ def test_datetime_outofbounds_scalar(self, value, format, warning):
11631163 assert res is NaT
11641164
11651165 if format is not None :
1166- msg = r'^time data ".*" at position 0 doesn\'t match format ".*"$'
1166+ msg = r'^time data ".*" doesn\'t match format ".*", at position 0 $'
11671167 with pytest .raises (ValueError , match = msg ):
11681168 to_datetime (value , errors = "raise" , format = format )
11691169 else :
1170- msg = "Out of bounds .* present at position 0"
1170+ msg = "^ Out of bounds .*, at position 0$ "
11711171 with pytest .raises (
11721172 OutOfBoundsDatetime , match = msg
11731173 ), tm .assert_produces_warning (warning , match = "Could not infer format" ):
@@ -1189,10 +1189,10 @@ def test_datetime_invalid_index(self, values, format, warning):
11891189
11901190 msg = "|" .join (
11911191 [
1192- r'^Given date string "a" not likely a datetime present at position 0$' ,
1193- r'^time data "a" at position 0 doesn\'t match format "%H:%M:%S"$' ,
1194- r'^unconverted data remains at position 0: "9" $' ,
1195- r"^second must be in 0..59: 00:01:99 present at position 0$" ,
1192+ r'^Given date string "a" not likely a datetime, at position 0$' ,
1193+ r'^time data "a" doesn\'t match format "%H:%M:%S", at position 0 $' ,
1194+ r'^unconverted data remains: "9", at position 0$' ,
1195+ r"^second must be in 0..59: 00:01:99, at position 0$" ,
11961196 ]
11971197 )
11981198 with pytest .raises (ValueError , match = msg ):
@@ -1372,7 +1372,7 @@ def test_to_datetime_malformed_raise(self):
13721372 ts_strings = ["200622-12-31" , "111111-24-11" ]
13731373 with pytest .raises (
13741374 ValueError ,
1375- match = r"^hour must be in 0\.\.23: 111111-24-11 present at position 1$" ,
1375+ match = r"^hour must be in 0\.\.23: 111111-24-11, at position 1$" ,
13761376 ):
13771377 with tm .assert_produces_warning (
13781378 UserWarning , match = "Could not infer format"
@@ -1845,8 +1845,8 @@ def test_dataframe_coerce(self, cache):
18451845 df2 = DataFrame ({"year" : [2015 , 2016 ], "month" : [2 , 20 ], "day" : [4 , 5 ]})
18461846
18471847 msg = (
1848- r'^cannot assemble the datetimes: time data ".+" at position 1 doesn\'t '
1849- r'match format "%Y%m%d"$'
1848+ r'^cannot assemble the datetimes: time data ".+" doesn\'t '
1849+ r'match format "%Y%m%d", at position 1 $'
18501850 )
18511851 with pytest .raises (ValueError , match = msg ):
18521852 to_datetime (df2 , cache = cache )
@@ -1923,8 +1923,8 @@ def test_dataframe_float(self, cache):
19231923 # float
19241924 df = DataFrame ({"year" : [2000 , 2001 ], "month" : [1.5 , 1 ], "day" : [1 , 1 ]})
19251925 msg = (
1926- r"^cannot assemble the datetimes: unconverted data remains at position "
1927- r'0: "1"$'
1926+ r"^cannot assemble the datetimes: unconverted data remains: "
1927+ r'"1", at position 0 $'
19281928 )
19291929 with pytest .raises (ValueError , match = msg ):
19301930 to_datetime (df , cache = cache )
@@ -1946,7 +1946,7 @@ def test_to_datetime_barely_out_of_bounds(self):
19461946 # in an in-bounds datetime
19471947 arr = np .array (["2262-04-11 23:47:16.854775808" ], dtype = object )
19481948
1949- msg = "Out of bounds .* present at position 0"
1949+ msg = "^ Out of bounds nanosecond timestamp: .*, at position 0"
19501950 with pytest .raises (OutOfBoundsDatetime , match = msg ):
19511951 with tm .assert_produces_warning (
19521952 UserWarning , match = "Could not infer format"
@@ -1985,8 +1985,8 @@ def test_to_datetime_iso8601_fails(self, input, format, exact):
19851985 with pytest .raises (
19861986 ValueError ,
19871987 match = (
1988- rf"time data \"{ input } \" at position 0 doesn't match format "
1989- rf"\" { format } \""
1988+ rf"time data \"{ input } \" doesn't match format "
1989+ rf"\" { format } \", at position 0 "
19901990 ),
19911991 ):
19921992 to_datetime (input , format = format , exact = exact )
@@ -2007,8 +2007,8 @@ def test_to_datetime_iso8601_exact_fails(self, input, format):
20072007 with pytest .raises (
20082008 ValueError ,
20092009 match = (
2010- rf"time data \"{ input } \" at position 0 doesn't match format "
2011- rf"\" { format } \""
2010+ rf"time data \"{ input } \" doesn't match format "
2011+ rf"\" { format } \", at position 0 "
20122012 ),
20132013 ):
20142014 to_datetime (input , format = format )
@@ -2046,8 +2046,8 @@ def test_to_datetime_iso8601_separator(self, input, format):
20462046 with pytest .raises (
20472047 ValueError ,
20482048 match = (
2049- rf"time data \"{ input } \" at position 0 doesn\'t match format "
2050- rf"\" { format } \""
2049+ rf"time data \"{ input } \" doesn\'t match format "
2050+ rf"\" { format } \", at position 0 "
20512051 ),
20522052 ):
20532053 to_datetime (input , format = format )
@@ -2115,7 +2115,7 @@ def test_to_datetime_on_datetime64_series(self, cache):
21152115 def test_to_datetime_with_space_in_series (self , cache ):
21162116 # GH 6428
21172117 ser = Series (["10/18/2006" , "10/18/2008" , " " ])
2118- msg = r'^time data " " at position 2 doesn\'t match format "%m/%d/%Y"$'
2118+ msg = r'^time data " " doesn\'t match format "%m/%d/%Y", at position 2 $'
21192119 with pytest .raises (ValueError , match = msg ):
21202120 to_datetime (ser , errors = "raise" , cache = cache )
21212121 result_coerce = to_datetime (ser , errors = "coerce" , cache = cache )
@@ -2386,8 +2386,8 @@ def test_dayfirst_warnings_invalid_input(self):
23862386 with pytest .raises (
23872387 ValueError ,
23882388 match = (
2389- r'^time data "03/30/2011" at position 1 doesn\'t match format '
2390- r'"%d/%m/%Y"$'
2389+ r'^time data "03/30/2011" doesn\'t match format '
2390+ r'"%d/%m/%Y", at position 1 $'
23912391 ),
23922392 ):
23932393 to_datetime (arr , dayfirst = True )
@@ -2457,8 +2457,8 @@ def test_to_datetime_inconsistent_format(self, cache):
24572457 data = ["01/01/2011 00:00:00" , "01-02-2011 00:00:00" , "2011-01-03T00:00:00" ]
24582458 ser = Series (np .array (data ))
24592459 msg = (
2460- r'^time data "01-02-2011 00:00:00" at position 1 doesn\'t match format '
2461- r'"%m/%d/%Y %H:%M:%S"$'
2460+ r'^time data "01-02-2011 00:00:00" doesn\'t match format '
2461+ r'"%m/%d/%Y %H:%M:%S", at position 1 $'
24622462 )
24632463 with pytest .raises (ValueError , match = msg ):
24642464 to_datetime (ser , cache = cache )
@@ -2581,11 +2581,49 @@ def test_day_not_in_month_raise(self, cache):
25812581 ):
25822582 to_datetime ("2015-02-29" , errors = "raise" , cache = cache )
25832583
2584- @pytest .mark .parametrize ("arg" , ["2015-02-29" , "2015-02-32" , "2015-04-31" ])
2585- def test_day_not_in_month_raise_value (self , cache , arg ):
2586- msg = f'time data "{ arg } " at position 0 doesn\' t match format "%Y-%m-%d"'
2584+ @pytest .mark .parametrize (
2585+ "arg, format, msg" ,
2586+ [
2587+ (
2588+ "2015-02-29" ,
2589+ "%Y-%m-%d" ,
2590+ '^time data "2015-02-29" doesn\' t match format "%Y-%m-%d", '
2591+ "at position 0$" ,
2592+ ),
2593+ (
2594+ "2015-29-02" ,
2595+ "%Y-%d-%m" ,
2596+ "^day is out of range for month, at position 0$" ,
2597+ ),
2598+ (
2599+ "2015-02-32" ,
2600+ "%Y-%m-%d" ,
2601+ '^time data "2015-02-32" doesn\' t match format "%Y-%m-%d", '
2602+ "at position 0$" ,
2603+ ),
2604+ (
2605+ "2015-32-02" ,
2606+ "%Y-%d-%m" ,
2607+ '^time data "2015-32-02" doesn\' t match format "%Y-%d-%m", '
2608+ "at position 0$" ,
2609+ ),
2610+ (
2611+ "2015-04-31" ,
2612+ "%Y-%m-%d" ,
2613+ '^time data "2015-04-31" doesn\' t match format "%Y-%m-%d", '
2614+ "at position 0$" ,
2615+ ),
2616+ (
2617+ "2015-31-04" ,
2618+ "%Y-%d-%m" ,
2619+ "^day is out of range for month, at position 0$" ,
2620+ ),
2621+ ],
2622+ )
2623+ def test_day_not_in_month_raise_value (self , cache , arg , format , msg ):
2624+ # https://github.com/pandas-dev/pandas/issues/50462
25872625 with pytest .raises (ValueError , match = msg ):
2588- to_datetime (arg , errors = "raise" , format = "%Y-%m-%d" , cache = cache )
2626+ to_datetime (arg , errors = "raise" , format = format , cache = cache )
25892627
25902628 @pytest .mark .parametrize (
25912629 "expected, format, warning" ,
@@ -2965,7 +3003,7 @@ def test_invalid_origins_tzinfo(self):
29653003 def test_incorrect_value_exception (self ):
29663004 # GH47495
29673005 with pytest .raises (
2968- ValueError , match = "Unknown string format: yesterday present at position 1"
3006+ ValueError , match = "Unknown string format: yesterday, at position 1"
29693007 ):
29703008 with tm .assert_produces_warning (
29713009 UserWarning , match = "Could not infer format"
@@ -2983,8 +3021,7 @@ def test_incorrect_value_exception(self):
29833021 def test_to_datetime_out_of_bounds_with_format_arg (self , format , warning ):
29843022 # see gh-23830
29853023 msg = (
2986- r"^Out of bounds nanosecond timestamp: 2417-10-10 00:00:00 "
2987- r"present at position 0$"
3024+ r"^Out of bounds nanosecond timestamp: 2417-10-10 00:00:00, at position 0$"
29883025 )
29893026 with pytest .raises (OutOfBoundsDatetime , match = msg ):
29903027 with tm .assert_produces_warning (warning , match = "Could not infer format" ):
0 commit comments