Skip to content

Commit a94fd28

Browse files
committed
BUG: pd.to_datetime with format doesn't work with pd.NA
1 parent 99cf794 commit a94fd28

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

pandas/core/tools/datetimes.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
ABCDataFrame,
5757
ABCSeries,
5858
)
59-
from pandas.core.dtypes.missing import notna
59+
from pandas.core.dtypes.missing import (
60+
isna,
61+
notna,
62+
)
6063

6164
from pandas.arrays import (
6265
DatetimeArray,
@@ -390,11 +393,17 @@ def _convert_listlike_datetimes(
390393
format = None
391394

392395
if format is not None:
393-
res = _to_datetime_with_format(
394-
arg, orig_arg, name, tz, format, exact, errors, infer_datetime_format
395-
)
396-
if res is not None:
397-
return res
396+
try:
397+
res = _to_datetime_with_format(
398+
arg, orig_arg, name, tz, format, exact, errors, infer_datetime_format
399+
)
400+
if res is not None:
401+
return res
402+
except ValueError:
403+
# GH#42957: ValueError: time data '<NA>'
404+
# does not match format '%Y%m%d%H%M%S'
405+
if isna(arg):
406+
format = None
398407

399408
assert format is None or infer_datetime_format
400409
utc = tz == "utc"

0 commit comments

Comments
 (0)