0

I have two columns, both containing a lot of time values.

0          23:54:00
1          00:02:00
2          00:18:00
3          00:15:00
4          00:24:00
             ...   
2818548    23:58:00
2818549    01:29:00
2818550    01:52:00
2818551    00:12:00
2818552    00:07:00
Name: DEPARTURE, Length: 2818553, dtype: object

0          00:05:00
1          00:10:00
2          00:20:00
3          00:20:00
4          00:25:00
             ...   
2818548    23:59:00
2818549    23:59:00
2818550    23:59:00
2818551    23:59:00
2818552    23:59:00
Name: SCHEDULED, Length: 2818553, dtype: object

I would like to create a new column with the difference between these 2 columns in minutes as a float. Trying to simply subtract one from the other gives me:

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

How would I go about realising this?

Adnos
  • 9
  • 1
  • 4
  • Does this answer your question? [subtract two times in python](https://stackoverflow.com/questions/5259882/subtract-two-times-in-python) – Nick Dec 06 '20 at 01:34

1 Answers1

0

Your data types are objects, so you're getting the error. You need to convert to datetime (there are several ways to do this).

input = """
Departure Scheduled
23:54:00 00:05:00
00:02:00 00:10:00
00:18:00 00:20:00
00:15:00 00:20:00
00:24:00 00:25:00"""

df = pd.read_csv(io.StringIO(input), sep=' ')
df['Delay'] = pd.to_datetime(df['Departure']) - pd.to_datetime(df['Scheduled'])
df['DelayMinutes'] = pd.to_timedelta(df['Delay']).astype('timedelta64[m]').astype(int)

Output:

In [603]: df['DelayMinutes']
Out[603]:
0    1429
1      -8
2      -2
3      -5
4      -1
Name: DelayMinutes, dtype: int32
Jonathan Leon
  • 5,440
  • 2
  • 6
  • 14