简体   繁体   中英

Difference between two datetime.time (HH:MM) values in minutes as a float

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM