Home > Mobile >  changing format of a string to calculate duration in python
changing format of a string to calculate duration in python

Time:08-17

I'm trying to calculate the duration time from column start time and end time. I have them in this format as string '00:00:00' I'm thinking of to replacing every ':' with '.' so I can easily subtract them as double type to calculate the duration.

To achieve this I tried this code:

startT=list()
endT=list()
for ind,row in df.iterrows():
 startT=(dataframe['Chat Session Initiation Time'].astype(str).str[10:19]).replace(':','.')
 endT=(dataframe['Chat Session End Time'].astype(str).str[10:19]).replace(':','.')

the substring I'm doing is to get the time alone from the text. this code does not replace the characters as I wished. I need help with that.

and If you have a better way to calculate the duration let me know kindly

CodePudding user response:

import pandas as pd

data = [
    [ 0, "00:00:00", "00:01:19" ],
    [ 1, "00:15:50", "00:45:19" ],
    [ 2, "01:30:05", "02:01:19" ],
    [ 3, "04:29:10", "05:00:00" ]
]

df = pd.DataFrame(data, columns=['index','start','stop'])
print(df)
df['start'] = pd.to_timedelta(df['start'])
df['stop'] = pd.to_timedelta(df['stop'])
df['diff'] = df['stop'] - df['start']
print(df)

Output:

   index     start      stop
0      0  00:00:00  00:01:19
1      1  00:15:50  00:45:19
2      2  01:30:05  02:01:19
3      3  04:29:10  05:00:00
   index           start            stop            diff
0      0 0 days 00:00:00 0 days 00:01:19 0 days 00:01:19
1      1 0 days 00:15:50 0 days 00:45:19 0 days 00:29:29
2      2 0 days 01:30:05 0 days 02:01:19 0 days 00:31:14
3      3 0 days 04:29:10 0 days 05:00:00 0 days 00:30:50

You can also use to_datetime, but that assumes today's date. The math still works out, it just prints other than what you expect.

  • Related