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
  How to convert dates in odd format to months lokhtar 2 926 Apr-17-2021, 11:54 AM
Last Post: lokhtar
  Timedelta - datetime.now PoduShychka 6 1,318 Mar-17-2021, 06:34 PM
Last Post: PoduShychka
  Timedelta - datetime.now PoduShychka 1 1,783 Mar-17-2021, 03:49 PM
Last Post: bowlofred
  Convert email addresses to VCF format jehoshua 2 2,482 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  Convert date integers (ex. 43831) to regular format Galven 2 1,135 Nov-15-2020, 11:38 PM
Last Post: bowlofred
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 1,062 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  timedelta object cannot be interpreted as integer palladium 5 3,942 Jan-30-2020, 02:54 PM
Last Post: palladium
  Convert SAS Dates Format in a loop MohanReddy 2 1,673 Apr-02-2019, 10:31 AM
Last Post: scidam
  code help to convert log file to csv format bmunagala 3 9,181 Jan-21-2019, 06:08 PM
Last Post: snippsat
  Convert text file date into specific format jacklee26 11 6,412 May-15-2018, 07:36 AM
Last Post: buran

Forum Jump:

User Panel Messages

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