@@ -227,19 +227,14 @@ def test_to_datetime_with_NA(self, data, format, expected):
227
227
result = to_datetime (data , format = format )
228
228
tm .assert_index_equal (result , expected )
229
229
230
- @pytest .mark .parametrize (
231
- "data, format, expected" ,
232
- [
233
- (["201010" , pd .NA ], None , DatetimeIndex (["2010-10-20" , "NaT" ])),
234
- ],
235
- )
236
- def test_to_datetime_with_NA_with_warning (self , data , format , expected ):
230
+ def test_to_datetime_with_NA_with_warning (self ):
237
231
# GH#42957
238
232
with tm .assert_produces_warning (
239
233
UserWarning ,
240
234
match = "Could not infer format" ,
241
235
):
242
- result = to_datetime (data , format = format )
236
+ result = to_datetime (["201010" , pd .NA ])
237
+ expected = DatetimeIndex (["2010-10-20" , "NaT" ])
243
238
tm .assert_index_equal (result , expected )
244
239
245
240
def test_to_datetime_format_integer (self , cache ):
@@ -356,7 +351,6 @@ def test_to_datetime_with_non_exact(self, cache):
356
351
],
357
352
)
358
353
def test_parse_nanoseconds_with_formula_no_warning (self , cache , arg ):
359
-
360
354
# GH8989
361
355
# truncating the nanoseconds when a format was provided
362
356
expected = to_datetime (arg , cache = cache )
@@ -372,7 +366,6 @@ def test_parse_nanoseconds_with_formula_no_warning(self, cache, arg):
372
366
],
373
367
)
374
368
def test_parse_nanoseconds_with_formula_with_warning (self , cache , arg ):
375
-
376
369
# GH8989
377
370
# truncating the nanoseconds when a format was provided
378
371
with tm .assert_produces_warning (UserWarning , match = "Could not infer format" ):
@@ -972,15 +965,13 @@ def test_datetime_invalid_datatype(self, arg):
972
965
973
966
@pytest .mark .parametrize ("value" , ["a" , "00:01:99" ])
974
967
@pytest .mark .parametrize ("infer" , [True , False ])
975
- @pytest .mark .parametrize ("format" , [None , "H%:M%:S%" ])
976
- def test_datetime_invalid_scalar (self , value , format , infer ):
968
+ @pytest .mark .parametrize (
969
+ "format,warning" , [(None , UserWarning ), ("H%:M%:S%" , None )]
970
+ )
971
+ def test_datetime_invalid_scalar (self , value , format , warning , infer ):
977
972
# GH24763
978
- if format is None :
979
- warn = UserWarning
980
- else :
981
- warn = None
982
973
with tm .assert_produces_warning (
983
- warn ,
974
+ warning ,
984
975
match = "Could not infer format" ,
985
976
):
986
977
res = to_datetime (
@@ -989,7 +980,7 @@ def test_datetime_invalid_scalar(self, value, format, infer):
989
980
assert res == value
990
981
991
982
with tm .assert_produces_warning (
992
- warn ,
983
+ warning ,
993
984
match = "Could not infer format" ,
994
985
):
995
986
res = to_datetime (
@@ -1003,22 +994,20 @@ def test_datetime_invalid_scalar(self, value, format, infer):
1003
994
f"Given date string { value } not likely a datetime"
1004
995
)
1005
996
with pytest .raises (ValueError , match = msg ):
1006
- with tm .assert_produces_warning (warn , match = "Could not infer format" ):
997
+ with tm .assert_produces_warning (warning , match = "Could not infer format" ):
1007
998
to_datetime (
1008
999
value , errors = "raise" , format = format , infer_datetime_format = infer
1009
1000
)
1010
1001
1011
1002
@pytest .mark .parametrize ("value" , ["3000/12/11 00:00:00" ])
1012
1003
@pytest .mark .parametrize ("infer" , [True , False ])
1013
- @pytest .mark .parametrize ("format" , [None , "H%:M%:S%" ])
1014
- def test_datetime_outofbounds_scalar (self , value , format , infer ):
1004
+ @pytest .mark .parametrize (
1005
+ "format,warning" , [(None , UserWarning ), ("H%:M%:S%" , None )]
1006
+ )
1007
+ def test_datetime_outofbounds_scalar (self , value , format , warning , infer ):
1015
1008
# GH24763
1016
- if format is None :
1017
- warn = UserWarning
1018
- else :
1019
- warn = None
1020
1009
with tm .assert_produces_warning (
1021
- warn ,
1010
+ warning ,
1022
1011
match = "Could not infer format" ,
1023
1012
):
1024
1013
res = to_datetime (
@@ -1027,7 +1016,7 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
1027
1016
assert res == value
1028
1017
1029
1018
with tm .assert_produces_warning (
1030
- warn ,
1019
+ warning ,
1031
1020
match = "Could not infer format" ,
1032
1021
):
1033
1022
res = to_datetime (
@@ -1042,31 +1031,26 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
1042
1031
value , errors = "raise" , format = format , infer_datetime_format = infer
1043
1032
)
1044
1033
else :
1045
- # TODO changes slightly, leave as GH comment
1046
- msg = r"^Out of bounds nanosecond timestamp: .*"
1047
- with pytest .raises (OutOfBoundsDatetime , match = msg ):
1048
- with tm .assert_produces_warning (
1049
- warn ,
1050
- match = "Could not infer format" ,
1051
- ):
1052
- to_datetime (
1053
- value ,
1054
- errors = "raise" ,
1055
- format = format ,
1056
- infer_datetime_format = infer ,
1057
- )
1034
+ msg = "Out of bounds .* present at position 0"
1035
+ with pytest .raises (
1036
+ OutOfBoundsDatetime , match = msg
1037
+ ), tm .assert_produces_warning (
1038
+ warning ,
1039
+ match = "Could not infer format" ,
1040
+ ):
1041
+ to_datetime (
1042
+ value , errors = "raise" , format = format , infer_datetime_format = infer
1043
+ )
1058
1044
1059
1045
@pytest .mark .parametrize ("values" , [["a" ], ["00:01:99" ], ["a" , "b" , "99:00:00" ]])
1060
1046
@pytest .mark .parametrize ("infer" , [True , False ])
1061
- @pytest .mark .parametrize ("format" , [None , "H%:M%:S%" ])
1062
- def test_datetime_invalid_index (self , values , format , infer ):
1047
+ @pytest .mark .parametrize (
1048
+ "format,warning" , [(None , UserWarning ), ("H%:M%:S%" , None )]
1049
+ )
1050
+ def test_datetime_invalid_index (self , values , format , warning , infer ):
1063
1051
# GH24763
1064
- if format is None :
1065
- warn = UserWarning
1066
- else :
1067
- warn = None
1068
1052
with tm .assert_produces_warning (
1069
- warn ,
1053
+ warning ,
1070
1054
match = "Could not infer format" ,
1071
1055
):
1072
1056
res = to_datetime (
@@ -1075,7 +1059,7 @@ def test_datetime_invalid_index(self, values, format, infer):
1075
1059
tm .assert_index_equal (res , Index (values ))
1076
1060
1077
1061
with tm .assert_produces_warning (
1078
- warn ,
1062
+ warning ,
1079
1063
match = "Could not infer format" ,
1080
1064
):
1081
1065
res = to_datetime (
@@ -1090,7 +1074,7 @@ def test_datetime_invalid_index(self, values, format, infer):
1090
1074
)
1091
1075
with pytest .raises (ValueError , match = msg ):
1092
1076
with tm .assert_produces_warning (
1093
- warn ,
1077
+ warning ,
1094
1078
match = "Could not infer format" ,
1095
1079
):
1096
1080
to_datetime (
@@ -1199,7 +1183,9 @@ def test_to_datetime_converts_null_like_to_nat(self, cache, input, expected):
1199
1183
(Series (["" ] * 60 ), Series ([NaT ] * 60 , dtype = "datetime64[ns]" )),
1200
1184
),
1201
1185
)
1202
- def test_to_datetime_converts_null_like_to_nat_warns (self , cache , input , expected ):
1186
+ def test_to_datetime_converts_null_like_to_nat_with_warning (
1187
+ self , cache , input , expected
1188
+ ):
1203
1189
# GH35888
1204
1190
with tm .assert_produces_warning (
1205
1191
UserWarning ,
@@ -1324,6 +1310,7 @@ def test_iso_8601_strings_with_different_offsets_utc(self):
1324
1310
1325
1311
def test_iso8601_strings_mixed_offsets_with_naive (self ):
1326
1312
# GH 24992
1313
+ # Can't parse consistently, need to parse each element in loop.
1327
1314
result = DatetimeIndex (
1328
1315
[
1329
1316
to_datetime (string , utc = True )
@@ -1350,6 +1337,7 @@ def test_iso8601_strings_mixed_offsets_with_naive(self):
1350
1337
1351
1338
def test_iso8601_strings_mixed_offsets_with_naive_reversed (self ):
1352
1339
items = ["2018-11-28T00:00:00+12:00" , "2018-11-28T00:00:00" ]
1340
+ # Can't parse consistently, need to parse each element in loop.
1353
1341
result = [to_datetime (item , utc = True ) for item in items ]
1354
1342
expected = [to_datetime (item , utc = True ) for item in list (reversed (items ))][::- 1 ]
1355
1343
assert result == expected
@@ -1514,34 +1502,20 @@ def test_unit_with_numeric(self, cache, errors, dtype):
1514
1502
result = to_datetime (arr , errors = errors , cache = cache )
1515
1503
tm .assert_index_equal (result , expected )
1516
1504
1517
- @pytest .mark .parametrize (
1518
- "exp, arr" ,
1519
- [
1520
- [
1521
- ["2015-06-19 05:33:20" , "2015-05-27 22:33:20" , "NaT" , "NaT" ],
1522
- [1.434692e18 , 1.432766e18 , "foo" , "NaT" ],
1523
- ],
1524
- ],
1525
- )
1526
- def test_unit_with_numeric_coerce (self , cache , exp , arr ):
1505
+ def test_unit_with_numeric_coerce (self , cache ):
1527
1506
# but we want to make sure that we are coercing
1528
1507
# if we have ints/strings
1508
+ exp = ["2015-06-19 05:33:20" , "2015-05-27 22:33:20" , "NaT" , "NaT" ]
1509
+ arr = [1.434692e18 , 1.432766e18 , "foo" , "NaT" ]
1529
1510
expected = DatetimeIndex (exp )
1530
1511
result = to_datetime (arr , errors = "coerce" , cache = cache )
1531
1512
tm .assert_index_equal (result , expected )
1532
1513
1533
- @pytest .mark .parametrize (
1534
- "exp, arr" ,
1535
- [
1536
- [
1537
- ["NaT" , "2015-06-19 05:33:20" , "2015-05-27 22:33:20" ],
1538
- ["foo" , 1.434692e18 , 1.432766e18 ],
1539
- ],
1540
- ],
1541
- )
1542
- def test_unit_with_numeric_coerce_warns (self , cache , exp , arr ):
1514
+ def test_unit_with_numeric_coerce_with_warning (self , cache ):
1543
1515
# but we want to make sure that we are coercing
1544
1516
# if we have ints/strings
1517
+ exp = ["NaT" , "2015-06-19 05:33:20" , "2015-05-27 22:33:20" ]
1518
+ arr = ["foo" , 1.434692e18 , 1.432766e18 ]
1545
1519
expected = DatetimeIndex (exp )
1546
1520
with tm .assert_produces_warning (
1547
1521
UserWarning ,
@@ -2300,13 +2274,17 @@ def test_to_datetime_infer_datetime_format_series_start_with_nans(self, cache):
2300
2274
)
2301
2275
2302
2276
@pytest .mark .parametrize (
2303
- "tz_name, offset" , [("UTC" , 0 ), ("UTC-3" , 180 ), ("UTC+3" , - 180 )]
2277
+ "tz_name, offset, warning" ,
2278
+ [("UTC" , 0 , None ), ("UTC-3" , 180 , UserWarning ), ("UTC+3" , - 180 , UserWarning )],
2304
2279
)
2305
- @pytest .mark .xfail (reason = "todo" , strict = False )
2306
- def test_infer_datetime_format_tz_name (self , tz_name , offset ):
2280
+ def test_infer_datetime_format_tz_name (self , tz_name , offset , warning ):
2307
2281
# GH 33133
2308
2282
ser = Series ([f"2019-02-02 08:07:13 { tz_name } " ])
2309
- result = to_datetime (ser , infer_datetime_format = True )
2283
+ with tm .assert_produces_warning (
2284
+ warning ,
2285
+ match = "Could not infer format" ,
2286
+ ):
2287
+ result = to_datetime (ser , infer_datetime_format = True )
2310
2288
expected = Series (
2311
2289
[Timestamp ("2019-02-02 08:07:13" ).tz_localize (pytz .FixedOffset (offset ))]
2312
2290
)
@@ -2347,25 +2325,17 @@ class TestDaysInMonth:
2347
2325
# tests for issue #10154
2348
2326
2349
2327
@pytest .mark .parametrize (
2350
- "arg, format" ,
2351
- [
2352
- ["2015-02-29" , "%Y-%m-%d" ],
2353
- ["2015-02-32" , "%Y-%m-%d" ],
2354
- ["2015-04-31" , "%Y-%m-%d" ],
2355
- ],
2356
- )
2357
- def test_day_not_in_month_coerce (self , cache , arg , format ):
2358
- assert isna (to_datetime (arg , errors = "coerce" , format = format , cache = cache ))
2359
-
2360
- @pytest .mark .parametrize (
2361
- "arg, format" ,
2328
+ "arg, format, warning" ,
2362
2329
[
2363
- ["2015-02-29" , None ],
2330
+ ["2015-02-29" , None , UserWarning ],
2331
+ ["2015-02-29" , "%Y-%m-%d" , None ],
2332
+ ["2015-02-32" , "%Y-%m-%d" , None ],
2333
+ ["2015-04-31" , "%Y-%m-%d" , None ],
2364
2334
],
2365
2335
)
2366
- def test_day_not_in_month_coerce_with_warning (self , cache , arg , format ):
2336
+ def test_day_not_in_month_coerce (self , cache , arg , format , warning ):
2367
2337
with tm .assert_produces_warning (
2368
- UserWarning ,
2338
+ warning ,
2369
2339
match = "Could not infer format" ,
2370
2340
):
2371
2341
assert isna (to_datetime (arg , errors = "coerce" , format = format , cache = cache ))
0 commit comments