parsing datetime beyond microseconds in Python in unknown ISO format

Question:

I’m trying to parse this datetime:

t = '2021-08-21 11:23:45.180999936'

using datetime strptime function:

from datetime import datetime

datetime.strptime(t, '%Y-%m-%d %H:%M:%S.%f').time()

I’m struggling with the last element of the datime, which I assume to be microseconds (%f), but get this error:

ValueError: unconverted data remains: 936 strptime

So if I understood the value error says the datetime is three digits too long for the last part to be a microsecond. What is the right way of parsing this datetime if not with microseconds? What is the ISO format of this datetime?

My question is related to this (unanswered) question with a different (related?) format (with Z-suffix).

Asked By: Des Grieux

||

Answers:

In python, strftime and strptime allow only up to 6 decimal places. They aren’t fully ISO 8601.

%f Microsecond as a decimal number, zero-padded to 6 digits.

Taken from datetime.datetime.fromisodatetime documentation:

Caution This does not support parsing arbitrary ISO 8601 strings – it is only intended as the inverse operation of datetime.isoformat(). A more full-featured ISO 8601 parser, dateutil.parser.isoparse is available in the third-party package dateutil.

Answered By: ljmc

There are 2 ways to parse the string to datetime or Timestamp objects

import pandas as pd
t = '2021-08-21 11:23:45.180999936'
t1 = pd.Timestamp(t)
t2 = pd.to_datetime(t)

The output is Timestamp object

Timestamp('2021-08-21 11:23:45.180999936')

Another way is using the library

from datetime import datetime
t = '2021-08-21 11:23:45.180999936'
t3 = datetime.fromisoformat(t.split('.')[0])
t4 = datetime.strptime(t.split('.')[0], '%Y-%m-%d %H:%M:%S')

The output is datetime object

datetime.datetime(2021, 8, 21, 11, 23, 45)
Answered By: danPho
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.