I have a df
with one column
displaying time in seconds. I'd like to convert those to hh:mm
or hh:mm:ss
.
If the time goes over standard 24hr time I'd still like it to be in hh:mm:ss
. Not 'n' days hh:mm:ss
.
To provide an example:
import pandas as pd
import numpy as np
import datetime
ts1 = ['21000', np.nan, '40000', np.nan, '49000', '100000']
ts2 = [0, 2, 'yy', 3, 'yy', 'yy']
ts3 = [0, 2, np.nan, 3, 4, np.nan]
d = {'X': ts1, 'Y': ts2, 'Z': ts3}
df = pd.DataFrame(data=d)
Output:
X Y Z
0 21000 0 0.0
1 NaN 2 2.0
2 40000 yy NaN
3 NaN 3 3.0
4 49000 yy 4.0
5 100000 yy NaN
I can perform this on a single string
using:
t = str(datetime.timedelta(seconds=21000))
Output t:
5:50:00
But how would I pass the same function to an entire column?
#t_col = str(datetime.timedelta(seconds=df['ts1']))
Intended Output:
X Y Z
0 5:50:00 0 0.0
1 NaN 2 2.0
2 11:06:40 yy NaN
3 Nan 3 3.0
4 13:36:40 yy 4.0
5 27:46:40 yy NaN