Home > Net >  ValueError: time data does not match format '%H:%M:%S.%f' (match)
ValueError: time data does not match format '%H:%M:%S.%f' (match)

Time:02-25

I have a dataframe with thousand of rows

df_11=

     dataReceived      time     
0       1           15:00:56.052554
1       1           15:02:18.764644
2       1           02:43:10 
3       1           05:46:12
...
6054    1           23:15:14.5687

When i'm trying to convert the column time into date time like this:

pd.to_datetime(df_11['time'], format='%H:%M:%S.%f')

I got the obvious error:

ValueError: time data '02:43:10' does not match format '%H:%M:%S.%f' (match)

Is their a way to easyly append .0 to certains values to fulfill the criteria? I think it's the best thing to do here

I found this similar problem here but i'd like to not use loop when working with dataframe

thx

CodePudding user response:

You have different options.

(1) Edit the text before parsing the date

from datetime import datetime

date_texts = [
    "15:00:56.052554",
    "02:43:10",
    "05:46:12",
    "23:15:14.5687"
]

for date_text in date_texts:
    if not '.' in date_text:
        date_text  = ".0"
    dt = datetime.strptime(date_text, "%H:%M:%S.%f")
    
    print(f"{date_text} --> {dt}")

(2) Check the date format first, then apply the correct format when parsing

from datetime import datetime
import re

date_texts = [
    "15:00:56.052554",
    "02:43:10",
    "05:46:12",
    "23:15:14.5687"
]

for date_text in date_texts:
    if re.match(r"^[0-9] :[0-9] :[0-9] \.[0-9] $", date_text):
        dt = datetime.strptime(date_text, "%H:%M:%S.%f")
    else:
        dt = datetime.strptime(date_text, "%H:%M:%S")

    print(f"{date_text} --> {dt}")

(3) Try the first format, catch the exception, then try the second format

from datetime import datetime

date_texts = [
    "15:00:56.052554",
    "02:43:10",
    "05:46:12",
    "23:15:14.5687"
]

for date_text in date_texts:
    try:
        dt = datetime.strptime(date_text, "%H:%M:%S.%f")
    except ValueError:
        dt = datetime.strptime(date_text, "%H:%M:%S")
    
    print(f"{date_text} --> {dt}")
    
  • Related