Skip to content

BUG: pd.to_datetime with format doesn't work with pd.NA #42982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 25, 2021
21 changes: 15 additions & 6 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@
ABCDataFrame,
ABCSeries,
)
from pandas.core.dtypes.missing import notna
from pandas.core.dtypes.missing import (
isna,
notna,
)

from pandas.arrays import (
DatetimeArray,
Expand Down Expand Up @@ -390,11 +393,17 @@ def _convert_listlike_datetimes(
format = None

if format is not None:
res = _to_datetime_with_format(
arg, orig_arg, name, tz, format, exact, errors, infer_datetime_format
)
if res is not None:
return res
try:
res = _to_datetime_with_format(
arg, orig_arg, name, tz, format, exact, errors, infer_datetime_format
)
if res is not None:
return res
except ValueError:
# GH#42957: ValueError: time data '<NA>'
# does not match format '%Y%m%d%H%M%S'
if isna(arg):
format = None

assert format is None or infer_datetime_format
utc = tz == "utc"
Expand Down