Tobitor
Tobitor

Reputation: 1508

strptime - ValueError: time data '02/08/18 10:47:43,907000000' does not match format '%d/%m/%Y %H:%M:%S,%f'

I have time data stored in my pandas dataframe as string and want to get it to datetime. One entry looks like this:

02/08/18 10:47:43,907000000

I use this code in order to get it done:

df['Datetime'] = df['TIME'].apply(lambda x: datetime.strptime(x, "%d/%m/%Y %H:%M:%S,%f"))

But I get this error:

ValueError: time data '02/08/18 10:47:43,907000000' does not match format '%d/%m/%Y %H:%M:%S,%f'

How do I have to modify my code?

Thanks a lot!

Upvotes: 1

Views: 61

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150825

Aside from the format error %Y vs %y, use pd.to_datetime instead of apply:

df['Datetime'] = pd.to_datetime(df['TIME'], format="%d/%m/%y %H:%M:%S,%f")

You can also let Pandas guess the format for you (which might be slower):

df['Datetime'] = pd.to_datetime(df['TIME'])

Upvotes: 1

mullinscr
mullinscr

Reputation: 1748

Your year formatter should be in lowercase (%y), not uppercase (%Y).

Giving:

'%d/%m/%y %H:%M:%S,%f'

Upvotes: 2

Related Questions