pythonpandastimetimedeltaseconds

How to subtract seconds column from timedelta64 column in pandas?


I have many columns containing onset and offset times. I would like to be able to subtract these columns from an overall device delay factor to get corrected offset and onset times. There seems to be an edge case, resulting in answers in this format: -1 days +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)

My columns containing times are in the format timedelta64[ns].

when I perform the subtraction, sometimes it works well, but there are cases where it is incorrect, seen in ID 7:

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

Absolute value does not fix the situation either.

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

How can I create a new column with the correct time differences?

the answers for IDs 2 and 13 seem to be correct but the answer for ID 7 should be different to what is displayed.


Solution

  • Try using diff instead:

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