在熊猫数据框中减去两个时钟时间

时间:2019-04-23 14:39:32

标签: python-3.x pandas

我试图减去包含正常时钟时间的大熊猫数据帧的两列作为字符串,但是不知何故我被打击了。

我尝试使用pandas.datetime将每一列转换为datetime,但是减法仍然无效。

import pandas as pd
df = pd.DataFrame()
df['A'] = ["12:30","5:30"]
df['B'] = ["19:30","9:30"]

df['A'] = pd.to_datetime(df['A']).dt.time
df['B'] = pd.to_datetime(df['B']).dt.time

df['time_diff'] = df['B'] - df['A']

I am expecting the actual time difference between two clock times.

2 个答案:

答案 0 :(得分:1)

您应该使用to_timedelta

df['A'] = pd.to_timedelta(df['A']+':00')
df['B'] = pd.to_timedelta(df['B']+':00')
df['time_diff'] = df['B'] - df['A']
df
Out[21]: 
         A        B time_diff
0 12:30:00 19:30:00  07:00:00
1 05:30:00 09:30:00  04:00:00

答案 1 :(得分:0)

我尝试了以下方法。这也为我工作。用3600除以小时即可得到时间。

 df = pd.DataFrame()
 df['A'] = ["12:30","5:30"]
 df['B'] = ["19:30","9:30"]
 df['time_diff_minutes'] = (pd.to_datetime(df['B']) - 
 pd.to_datetime(df['A'])).astype('timedelta64[s]')/60
 df['time_diff_hours'] = df['time_diff_minutes']/60
 df

Out[161]: 
       A      B  time_diff_minutes  time_diff_hours
0  12:30  19:30              420.0              7.0
1   5:30   9:30              240.0              4.0