Home > Software engineering >  Dealing with " 00:00" in datetime format
Dealing with " 00:00" in datetime format

Time:10-10

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:

  1. pd.concat([df, df.date_string.apply(lambda s: pd.Series({'date':datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f%z')}))], axis=1)
  2. pd.concat([df, df.file_created.apply(lambda s: pd.Series({'date':datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f.%z')}))], axis=1)
  3. pd.concat([df, df.file_created.apply(lambda s: pd.Series({'date':datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f:%z')}))], axis=1)

I get the error - time data '2020-06-30 15:20:13.078196 00:00' does not match format in all cases. Any help is appreciated.

CodePudding user response:

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).

CodePudding user response:

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.

  • Related