@@ -215,6 +215,7 @@ def _maybe_cache(
215
215
cache_array : Series
216
216
Cache of converted, unique dates. Can be empty
217
217
"""
218
+
218
219
from pandas import Series
219
220
220
221
cache_array = Series (dtype = object )
@@ -391,7 +392,6 @@ def _convert_listlike_datetimes(
391
392
raise TypeError (
392
393
"arg must be a string, datetime, list, tuple, 1-d array, or Series"
393
394
)
394
-
395
395
# warn if passing timedelta64, raise for PeriodDtype
396
396
# NB: this must come after unit transformation
397
397
orig_arg = arg
@@ -411,7 +411,6 @@ def _convert_listlike_datetimes(
411
411
412
412
if infer_datetime_format and format is None :
413
413
format = _guess_datetime_format_for_array (arg , dayfirst = dayfirst )
414
-
415
414
if format is not None :
416
415
# There is a special fast-path for iso8601 formatted
417
416
# datetime strings, so in those cases don't use the inferred
@@ -428,7 +427,6 @@ def _convert_listlike_datetimes(
428
427
)
429
428
if res is not None :
430
429
return res
431
-
432
430
assert format is None or infer_datetime_format
433
431
utc = tz == "utc"
434
432
result , tz_parsed = objects_to_datetime64ns (
@@ -440,7 +438,6 @@ def _convert_listlike_datetimes(
440
438
require_iso8601 = require_iso8601 ,
441
439
allow_object = True ,
442
440
)
443
-
444
441
if tz_parsed is not None :
445
442
# We can take a shortcut since the datetime64 numpy array
446
443
# is in UTC
@@ -495,6 +492,8 @@ def _array_strptime_with_fallback(
495
492
else :
496
493
if "%Z" in fmt or "%z" in fmt :
497
494
return _return_parsed_timezone_results (result , timezones , tz , name )
495
+ if infer_datetime_format and np .isnan (result ).any ():
496
+ return None
498
497
499
498
return _box_as_indexlike (result , utc = utc , name = name )
500
499
@@ -513,7 +512,6 @@ def _to_datetime_with_format(
513
512
Try parsing with the given format, returning None on failure.
514
513
"""
515
514
result = None
516
-
517
515
# shortcut formatting here
518
516
if fmt == "%Y%m%d" :
519
517
# pass orig_arg as float-dtype may have been converted to
@@ -1029,6 +1027,7 @@ def to_datetime(
1029
1027
'2020-01-01 18:00:00+00:00', '2020-01-01 19:00:00+00:00'],
1030
1028
dtype='datetime64[ns, UTC]', freq=None)
1031
1029
"""
1030
+
1032
1031
if arg is None :
1033
1032
return None
1034
1033
0 commit comments