-1

I have a dataframe df with first (min) and last (max) of request time of a day and I want to build the difference, so that I get the workhours of each day.

                 min       max
dates                         
2006-09-11  10:58:50  23:02:05
2006-09-12  00:59:26  19:06:29
2006-09-13  09:10:32  21:50:56
2006-09-14  10:25:43  22:40:13
2006-09-18  10:43:29  21:00:54
2006-09-19  08:21:37  21:05:41
2006-09-20  08:10:12  22:50:45
2006-09-21  10:29:40  22:16:23
2006-09-25  06:56:57  20:19:48
2006-09-26  00:42:25  21:31:07
2006-09-27  01:10:49  22:20:49
2006-09-28  09:55:26  20:58:46
2006-10-02  10:38:39  23:08:32
2006-10-03  09:44:46  23:20:40
2006-10-04  10:34:04  23:51:21
2006-10-05  10:04:50  23:43:43
2006-10-09  10:01:59  21:14:20
2006-10-10  09:58:45  18:48:06
2006-10-11  09:39:44  19:12:39
2006-10-12  02:34:16  23:08:57
2006-10-16  09:44:53  20:37:10
2006-10-17  10:24:33  19:09:37
2006-10-18  00:18:08  22:19:42

I'd like to have a new column df['wh'] where I have the differences between the two times. No of the suggestion I found could help me.. Thanks for any help!

temp123
  • 362
  • 1
  • 4
Melli
  • 1
  • 6

1 Answers1

0

We will have to convert your time strings to time format for difference function to work in your case. After that we just have to change the type back to string

I believe below code might work for you

import pandas as pd
df['min_as_time_format'] = pd.to_datetime(df['min'], format="%H:%M:%S")
df['max_as_time_format'] = pd.to_datetime(df['max'], format="%H:%M:%S")
df['diff'] = df['max_as_time_format'] - df['min_as_time_format']
df['diff'] = df['diff'].astype(str).str[-18:-10]

Let me know if it works for you

Shrey
  • 1,242
  • 1
  • 13
  • 27