Home > Software engineering >  parsing datetime beyond microseconds in Python in unknown ISO format
parsing datetime beyond microseconds in Python in unknown ISO format

Time:08-11

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

CodePudding user response:

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.

CodePudding user response:

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)
  • Related