简体   繁体   English

如何用timedelta(python)减去两个时间列?

[英]How to subtract two time columns with timedelta (python)?

I'm trying for hours to do a subtraction between this two time columns so I can see how long did it take to the other action happen: 我试图花几个小时在这两个时间列之间进行减法,以便了解其他操作花费了多长时间:

In[1]:aumento_data_separada 
Out[1]: Response_Time      Request_Time(when the client do a request)   
 0   00:56:58.612000    00:46:34.347000
 1   12:00:41.069000    12:00:32.603000
 2   18:05:02.776000    16:39:42.682000
 3   19:27:11.997000    17:33:05.589000 
...

This dataframe has more 392560 rows like these above. 此数据框具有更多上述392560行。 I'd like to do the operation in all of them. 我想对所有这些人进行手术。 and creat a new column with the result. 并使用结果创建一个新列。 I tried reading the docs from timedelta, but I didn't understand it at all. 我尝试从timedelta中阅读文档,但是我一点也不明白。 I did this -> type(aumento_data_separada['Response_Time'].iloc[0]) and type(aumento_data_separada['Request_Time'].iloc[0]) to see the type and it returned me a datetime.time . 我这样做-> type(aumento_data_separada['Response_Time'].iloc[0])type(aumento_data_separada['Request_Time'].iloc[0])来查看类型,它返回了一个datetime.time I found out that I can't do operations within them because of the type. 我发现由于类型的原因我无法在其中进行操作。 So I tried to convert them separately: pd.to_datetime(aumento_data_separada['Request_Time']) but it returns me the following error-> object of type 'datetime.time' has no len() . 所以我尝试分别转换它们: pd.to_datetime(aumento_data_separada['Request_Time'])但它返回以下错误消息-> object of type 'datetime.time' has no len()

Here is the 1st error message and the code I tried -> aumento_data_separada.Response_Time - aumento_data_separada.Request_Time - returning unsupported operand type(s) for -: 'datetime.time' and 'datetime.time' 这是第一条错误消息,也是我尝试的代码-> aumento_data_separada.Response_Time - aumento_data_separada.Request_Time返回unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

convert the columns to pd.Timedelta with pd.to_timedelta 列转换为pd.Timedeltapd.to_timedelta

df = df.apply(pd.to_timedelta)

df.Response_Time - df.Request_Time

0   00:10:24.265000
1   00:00:08.466000
2   01:25:20.094000
3   01:54:06.408000
dtype: timedelta64[ns]

This is similar to piRSquared's answer, but for some reason this is faster when I test the two methods side-by-side. 这类似于piRSquared的答案,但是由于某些原因,当我并排测试这两种方法时,这会更快。 Not sure why that is. 不知道为什么。

df['Time_Diff'] = pd.to_datetime(df.Response_Time) - pd.to_datetime(df.Request_Time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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