Skip to content

Commit 0d320e9

Browse files
BUG: Fixed behavior with fallback between raise and coerce pandas-dev#46071
1 parent 37e6239 commit 0d320e9

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

pandas/core/tools/datetimes.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def _maybe_cache(
215215
cache_array : Series
216216
Cache of converted, unique dates. Can be empty
217217
"""
218+
218219
from pandas import Series
219220

220221
cache_array = Series(dtype=object)
@@ -391,7 +392,6 @@ def _convert_listlike_datetimes(
391392
raise TypeError(
392393
"arg must be a string, datetime, list, tuple, 1-d array, or Series"
393394
)
394-
395395
# warn if passing timedelta64, raise for PeriodDtype
396396
# NB: this must come after unit transformation
397397
orig_arg = arg
@@ -411,7 +411,6 @@ def _convert_listlike_datetimes(
411411

412412
if infer_datetime_format and format is None:
413413
format = _guess_datetime_format_for_array(arg, dayfirst=dayfirst)
414-
415414
if format is not None:
416415
# There is a special fast-path for iso8601 formatted
417416
# datetime strings, so in those cases don't use the inferred
@@ -428,7 +427,6 @@ def _convert_listlike_datetimes(
428427
)
429428
if res is not None:
430429
return res
431-
432430
assert format is None or infer_datetime_format
433431
utc = tz == "utc"
434432
result, tz_parsed = objects_to_datetime64ns(
@@ -440,7 +438,6 @@ def _convert_listlike_datetimes(
440438
require_iso8601=require_iso8601,
441439
allow_object=True,
442440
)
443-
444441
if tz_parsed is not None:
445442
# We can take a shortcut since the datetime64 numpy array
446443
# is in UTC
@@ -495,6 +492,8 @@ def _array_strptime_with_fallback(
495492
else:
496493
if "%Z" in fmt or "%z" in fmt:
497494
return _return_parsed_timezone_results(result, timezones, tz, name)
495+
if infer_datetime_format and np.isnan(result).any():
496+
return None
498497

499498
return _box_as_indexlike(result, utc=utc, name=name)
500499

@@ -513,7 +512,6 @@ def _to_datetime_with_format(
513512
Try parsing with the given format, returning None on failure.
514513
"""
515514
result = None
516-
517515
# shortcut formatting here
518516
if fmt == "%Y%m%d":
519517
# pass orig_arg as float-dtype may have been converted to
@@ -1029,6 +1027,7 @@ def to_datetime(
10291027
'2020-01-01 18:00:00+00:00', '2020-01-01 19:00:00+00:00'],
10301028
dtype='datetime64[ns, UTC]', freq=None)
10311029
"""
1030+
10321031
if arg is None:
10331032
return None
10341033

0 commit comments

Comments
 (0)