Skip to content Skip to sidebar Skip to footer

Dealing With "+00:00" In Datetime Format

How do you convert a column of dates of the form '2020-06-30 15:20:13.078196+00:00' to datetime in pandas? This is what I have done: pd.concat([df, df.date_string.apply(lambda s:

Solution 1:

The easiest thing to do is let pd.to_datetime auto-infer the format. That works very well for standard formats like this (ISO 8601):

import pandas as pd

dti = pd.to_datetime(["2020-06-30 15:20:13.078196+00:00"])

print(dti)
# DatetimeIndex(['2020-06-30 15:20:13.078196+00:00'], dtype='datetime64[ns, UTC]', freq=None)

+00:00 is a UTC offset of zero hours, thus can be interpreted as UTC.

btw., pd.to_datetime also works very well for mixed formats, see e.g. here.

Solution 2:

None of the formats mentioned by you above matches your sample. Try this

"%Y-%m-%d %H:%M:%S.%f%z" (Notice the space before %H).

Post a Comment for "Dealing With "+00:00" In Datetime Format"