I have a dataframe which looks like below:
| ID | Date | Time |
|---|---|---|
| 1 | 20210501 | 15:09:34 |
| 1 | 20210501 | 15:19:34 |
| 2 | 20210501 | 17:58:03 |
| 2 | 20210501 | 17:52:30 |
| 2 | 20210501 | 17:52:28 |
I need to get a new column say Duration which should give me the difference of time on a date grouped by ID. e.g.
| ID | Date | Duration |
|---|---|---|
| 1 | 20210501 | 00:10:00 |
| 2 | 20210501 | 02:06:02 |
I converted Time to timedelta using below code however, it gives me
"ValueError: unit must not be specified if the input contains a str"
Code to convert Time to timedelta type:
data['Time'] = pd.to_timedelta(data['Creation Time'],unit='h')
Code to get expected dataframe is:
data['duration'] = data.sort_values('ID,'Date','Time').groupby(['ID','Date'])['Time'].diff()
Error:
ValueError: No axis named Creation Date for object type DataFrame
Please let me know how can I solve this and where I'm wrong.