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

2024-04-25 20:41:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有许多列包含起始时间和偏移时间。我希望能够从总的设备延迟因子中减去这些列,得到校正的偏移量和开始时间。似乎存在一个边缘案例,导致答案的格式为:-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的答案应该与显示的内容不同