richie
richie

Reputation: 18648

Find 'Time Delayed' using python pandas

I have the following dataframe;

Group     Deadline Time    Deadline Date    Task Completed Date   Task Completed Time
Group 1   20:00:00         17-07-2012       17-07-2012              20:34:00
Group 2   20:15:00         17-07-2012       17-07-2012              20:39:00
Group 3   22:00:00         17-07-2012       17-07-2012              22:21:00
Group 4   23:50:00         17-07-2012       18-07-2012              00:09:00
Group 5   20:00:00         18-07-2012       18-07-2012              20:37:00
Group 6   20:15:00         18-07-2012       18-07-2012              21:13:00
Group 7   22:00:00         18-07-2012       18-07-2012              22:56:00
Group 8   23:50:00         18-07-2012       19-07-2012              00:01:00
Group 9   20:15:00         19-07-2012       19-07-2012              20:34:00
Group 10  20:00:00         19-07-2012       19-07-2012              20:24:00

How do I calculate the time delay as;

Time Delay (mins)
00:34:00
00:24:00
00:21:00
00:19:00
00:37:00
00:58:00
00:56:00
00:11:00
00:19:00
00:24:00

I have tried without success;

  1. Combining the 'Deadline' 'date' & 'time' columns and 'Task Completed' 'date' & 'time' columns and

  2. Finding the difference as 'Task Completed' - 'Deadline' time.

Upvotes: 2

Views: 1015

Answers (1)

Dan Allan
Dan Allan

Reputation: 35265

Combine them as strings ("addition" works), convert them to datetime type, and then subtract, which gives a Series of timedelta type.

In [14]: deadline = pd.to_datetime(df['Deadline Date'] + ' ' + df['Deadline Time'])

In [15]: completed = pd.to_datetime(df['Task Completed Date'] + ' ' + df['Task Completed Time'])

In [16]: completed - deadline
Out[16]: 
0   00:34:00
1   00:24:00
2   00:21:00
3   00:19:00
4   00:37:00
5   00:58:00
6   00:56:00
7   00:11:00
8   00:19:00
9   00:24:00
dtype: timedelta64[ns]

Upvotes: 3

Related Questions