如何从 pandas 中的 timedelta64 列中减去秒列?

How to subtract seconds column from timedelta64 column in pandas?

我有很多包含起始时间和偏移时间的列。我希望能够从整个设备延迟因子中减去这些列以获得更正的偏移和开始时间。似乎有一个边缘案例,导致这种格式的答案:-1 天 +23:34:30.300000.

import pandas as pd
import numpy as np 
example=[["2","0 days 00:00:57.3","0 days 00:01:12.9","00:00:50.2","pos"],
 ["13","0 days 00:30:08.5","0 days 00:32:14.0", "00:20:28.0","neg"],
 ["6","0 days 00:27:18.7","0 days 00:01:24.2","0 days 00:26:48.4","pos"],
 ["7","0 days 00:01:56.676000","0 days 00:04:56.2","0 days 00:15:33.455000","pos"]]

example_table= pd.DataFrame(example,columns["ID","Start_Time","End_Time","Factor","tag"]) 
example_table.Factor=example_table.Factor.apply(pd.to_timedelta)

我包含时间的列的格式为 timedelta64[ns]。

我做减法的时候,有时效果很好,但也有不正确的情况,见ID 7:

example_table["x"]=(example_table["Start_Time"]-example_table["Factor"])

绝对值也不能解决问题。

example_table["absolute?"]=abs(example_table["Start_Time"]-example_table["Factor"])

如何创建具有正确时差的新列?

ID 2 和 13 的答案似乎是正确的,但 ID 7 的答案应该与显示的不同。

尝试使用 diff 代替:

example_table["x"] = example_table[["Start_Time", "Factor"]].diff(axis=1)['Factor']