0

I'm having trouble converting string variables into timedelta variable. According to this question: ValueError: time data 'In 00 days 23:07:56' does not match format 'In %d days %H:%M:%S'

you can solve it by changing 00 days into 01 days because date can't be 0, but in my case, that variable is the output of time difference calculations. Is there another solution without changing my variable.

This is the code:

import datetime
import pandas as pd

df = pd.read_excel('test.xlsx')
print(df.iloc[4,1])
print(type(df.iloc[4,1]))
t = datetime.datetime.strptime((df.iloc[4,1]),"%d days %H:%M:%S")

output before the error:

0 days 00:07:02.710529
<class 'str'>

error:

 File "C:\Users\khou\AppData\Local\Continuum\anaconda3\lib\_strptime.py", line 359, in _strptime
    (data_string, format))

ValueError: time data '0 days 00:07:02.710529' does not match format '%d days %H:%M:%S'
newbie
  • 646
  • 8
  • 27
  • first you should check what value you get if you use `"%d days %H:%M:%S"` to getnerate time - `import time` `time.strftime("%d days %H:%M:%S")` . It gives me `'25 days 16:13:14'` and there is no milliseconds. You may need `%S.%s` OR you may have to remove milliseconds. – furas Nov 25 '19 at 15:14
  • [How can I parse a time string containing milliseconds in it with python?](https://stackoverflow.com/questions/698223/how-can-i-parse-a-time-string-containing-milliseconds-in-it-with-python) – furas Nov 25 '19 at 15:20
  • it seems like it would help me out, but when I tried to test it, it raised an error `AttributeError: 'NoneType' object has no attribute 'groupdict'` when I googled it it advised me to put a null check to avoid it `try: if !(match is None):` `d = match.groupdict()` but I don't know where to place it exactly – newbie Nov 25 '19 at 15:48

0 Answers0