4

I have a column with timedelta and I would like to create an extra column extracting the hour and minute from the timedelta column.

df

time_delta          hour_minute
02:51:21.401000     2h:51min
03:10:32.401000     3h:10min
08:46:43.401000     08h:46min

This is what I have tried so far:

df['rh'] = df.time_delta.apply(lambda x: round(pd.Timedelta(x).total_seconds() \
                          % 86400.0 / 3600.0) )

Unfortunately, I'm not quite sure how to extract the minutes without incl. the hour

user12625679
  • 676
  • 8
  • 23
  • What is `dtype` of your `time_delta` column? Can you print output of `df['time_delta'].dtypes` ? – Abhilash Awasthi Aug 24 '20 at 11:42
  • Does this answer your question? [Formatting timedelta objects](https://stackoverflow.com/questions/8906926/formatting-timedelta-objects) – Stefan Aug 24 '20 at 11:44

2 Answers2

5

Use Series.dt.components for get hours and minutes and join together:

td = pd.to_timedelta(df.time_delta).dt.components
df['rh'] = (td.hours.astype(str).str.zfill(2) + 'h:' + 
            td.minutes.astype(str).str.zfill(2) + 'min')
print (df)
        time_delta hour_minute         rh
0  02:51:21.401000    2h:51min  02h:51min
1  03:10:32.401000    3h:10min  03h:10min
2  08:46:43.401000   08h:46min  08h:46min

If possible values of hour are more like 24hours is necessary also add days:

print (df)
        time_delta hour_minute
0  02:51:21.401000    2h:51min
1  03:10:32.401000    3h:10min
2  28:46:43.401000   28h:46min

td = pd.to_timedelta(df.time_delta).dt.components
print (td)
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0     0      2       51       21           401             0            0
1     0      3       10       32           401             0            0
2     1      4       46       43           401             0            0

df['rh'] = ((td.days * 24 + td.hours).astype(str).str.zfill(2) + 'h:' + 
            td.minutes.astype(str).str.zfill(2) + 'min')
print (df)

        time_delta hour_minute         rh
0  02:51:21.401000    2h:51min  02h:51min
1  03:10:32.401000    3h:10min  03h:10min
2  28:46:43.401000   28h:46min  28h:46min
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

See also this post which defines the function

def strfdelta(tdelta, fmt):
    d = {"days": tdelta.days}
    d["hours"], rem = divmod(tdelta.seconds, 3600)
    d["minutes"], d["seconds"] = divmod(rem, 60)
    return fmt.format(**d)

Then, e.g.

strfdelta(pd.Timedelta('02:51:21.401000'), '{hours}h:{minutes}min')

gives '2h:51min'. For your full dataframe

df['rh'] = df.time_delta.apply(lambda x: strfdelta(pd.Timedelta(x), '{hours}h:{minutes}min'))
Stefan
  • 1,697
  • 15
  • 31