1

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.

zob
  • 118
  • 14
  • isn't `0 days 00:00:30.300000` is correct for id 6 – Anurag Dabas Aug 22 '21 at 05:05
  • @Anurag Dabas whoops-- I had made an edit and did not thoroughly check before submitting. Yes 6 is correct, but 7 is not. When I have additional columns, depending on what I use the -1 day shows up or the incorrect time difference shows up. – zob Aug 22 '21 at 05:09
  • also `0 days 00:27:30.300000` is correct for id 7 – Anurag Dabas Aug 22 '21 at 05:11
  • 1
    @Anurag Dabas oof. Ok I have been looking at this incorrectly. Thank you so much. I was making an error in a different area that was causing this, and the display is difficult to read so I was making errors in what I thought was correct. – zob Aug 22 '21 at 05:20
  • And yes you are getting correct values because that's is displaying like that but the actual value is different and you can check that by `example_table.loc[3,'Factor']` and `example_table.loc[3,'Start_Time']` – Anurag Dabas Aug 22 '21 at 05:27

1 Answers1

1

Try using diff instead:

example_table["x"] = example_table[["Start_Time", "Factor"]].diff(axis=1)['Factor']
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Thank you for your response but I'm not sure how to use that to have control over what I would like to end up in the x column. I would like to subtract the Start_Time from the Factor column. I have edited the question slightly to show that the code is still not getting at quite the right answer. – zob Aug 22 '21 at 04:38
  • U12-Forward So possibly closer but it gives me: KeyError: 'End_Time' ... – zob Aug 22 '21 at 04:43