Python Forum
Convert timedelta to specified format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Convert timedelta to specified format (/thread-34106.html)



Convert timedelta to specified format - Planetary_Assault_Systems - Jun-27-2021

Hello

I am currently attempting to create a web dashboard for analytics in Formula1 using plotly and flask as per the article
An Interactive Web Dashboard with Plotly and Flask.

I have lap times in string format of MM:SS:sss (where MM is minutes and sss is milliseconds) and I have attempted (python script below) to convert this to quantifiable values using datetime.timedelta so that I am able to graph them and manipulate them (e.g find the average time of a driver over a number of laps). However when I try graphing the timedelta objects in plotly, they are displayed in microseconds.

times = ["1:23.921", "1:24.690", "1:24.790"]

# convert to timedelta object
def string_con(string_time):
    new_time = timedelta(minutes=int(string_time.split(
        ":")[0]), seconds=int((string_time.split(":")[1]).split(".")[0]),
        milliseconds=int((string_time.split(":")[1]).split(".")[1]))
    return new_time

# convert to timedelta object - list
def string_con_list(string_times):
    laps = list(map(string_con, string_times))
    return laps


# compute average pace using timedelta objects
def average_pace(laps):
    laps = list(map(string_con, laps))
    return (sum(laps, timedelta(0))/len(laps))


print(string_con_list(times))
print(average_pace(times))
In seeing the code above, I want the string_con_list() function to return a list of timedelta objects in MM:SS:sss format. Please may someone point me in the right direction. Thank you.


RE: Convert timedelta to specified format - snippsat - Jun-27-2021

(Jun-27-2021, 08:08 AM)Planetary_Assault_Systems Wrote: I want the string_con_list() function to return a list of timedelta objects in MM:SS:sss format. Please may someone point me in the right direction. Thank you.
Something like this.
>>> from datetime import datetime
>>> 
>>> con_lst = string_con_list(times)
>>> con_lst
[datetime.timedelta(seconds=83, microseconds=921000),
 datetime.timedelta(seconds=84, microseconds=690000),
 datetime.timedelta(seconds=84, microseconds=790000)]

>>> dt = datetime.strptime(str(con_lst[0]), "%H:%M:%S.%f")
>>> dt
datetime.datetime(1900, 1, 1, 0, 1, 23, 921000)
>>> dt.minute
1
>>> dt.second
23
>>> dt.microsecond
921000

>>> dt.strftime('%M:%S.%f')
'01:23.921000'

# Works also if put date object in a f-string
>>> print(f'{dt.minute}:{dt.second}:{dt.microsecond}')
1:23:921000



RE: Convert timedelta to specified format - Planetary_Assault_Systems - Jun-27-2021

(Jun-27-2021, 10:49 AM)snippsat Wrote:
(Jun-27-2021, 08:08 AM)Planetary_Assault_Systems Wrote: I want the string_con_list() function to return a list of timedelta objects in MM:SS:sss format. Please may someone point me in the right direction. Thank you.
Something like this.
>>> from datetime import datetime
>>> 
>>> con_lst = string_con_list(times)
>>> con_lst
[datetime.timedelta(seconds=83, microseconds=921000),
 datetime.timedelta(seconds=84, microseconds=690000),
 datetime.timedelta(seconds=84, microseconds=790000)]

>>> dt = datetime.strptime(str(con_lst[0]), "%H:%M:%S.%f")
>>> dt
datetime.datetime(1900, 1, 1, 0, 1, 23, 921000)
>>> dt.minute
1
>>> dt.second
23
>>> dt.microsecond
921000

>>> dt.strftime('%M:%S.%f')
'01:23.921000'

# Works also if put date object in a f-string
>>> print(f'{dt.minute}:{dt.second}:{dt.microsecond}')
1:23:921000

Hello snippsat, thank you for your response. I am aware of the strftime() function but if I convert the timedelta to a string, I would not be able to plot it as y-values on a graph. Please correct me if im wrong. I have attached a picture of the exact graph I am trying to construct.


RE: Convert timedelta to specified format - snippsat - Jun-27-2021

I guess can calculate with timedelta object in plot then display the correlation date in string format.
Example.
>>> con_lst
[datetime.timedelta(seconds=83, microseconds=921000),
 datetime.timedelta(seconds=84, microseconds=690000),
 datetime.timedelta(seconds=84, microseconds=790000)]

>>> con_lst_date = [str(d)[3:] for d in con_lst]
>>> con_lst_date
['1:23.921000', '1:24.690000', '1:24.790000']
>>> 
>>> list(zip(con_lst_date, con_lst))
[('1:23.921000', datetime.timedelta(seconds=83, microseconds=921000)),
 ('1:24.690000', datetime.timedelta(seconds=84, microseconds=690000)),
 ('1:24.790000', datetime.timedelta(seconds=84, microseconds=790000))]
>>> 
>>> dict(zip(con_lst_date, con_lst))
{'1:23.921000': datetime.timedelta(seconds=83, microseconds=921000),
 '1:24.690000': datetime.timedelta(seconds=84, microseconds=690000),
 '1:24.790000': datetime.timedelta(seconds=84, microseconds=790000)}