Python Forum
Convert timedelta to specified format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert timedelta to specified format
#1
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.
Reply
#2
(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
Reply
#3
(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.
Reply
#4
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)}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 705 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Convert Json to table format python_student 2 5,071 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert .xlsx to Format as Table bnadir55 0 862 Aug-11-2022, 06:39 AM
Last Post: bnadir55
  how to convert and format a varable darktitan 4 1,624 May-29-2022, 12:59 PM
Last Post: darktitan
  Convert Date to another format lonesoac0 2 1,632 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  How to convert dates in odd format to months lokhtar 2 2,183 Apr-17-2021, 11:54 AM
Last Post: lokhtar
  Timedelta - datetime.now PoduShychka 6 3,432 Mar-17-2021, 06:34 PM
Last Post: PoduShychka
  Timedelta - datetime.now PoduShychka 1 5,466 Mar-17-2021, 03:49 PM
Last Post: bowlofred
  Convert email addresses to VCF format jehoshua 2 4,610 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  Convert date integers (ex. 43831) to regular format Galven 2 2,553 Nov-15-2020, 11:38 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020