0

I'm making a function to calculate the time difference between two durations using Pandas.

The function is:

def time_calc(dur1, dur2):
    date1 = pd.to_datetime(pd.Series(dur2))
    date2 = pd.to_datetime(pd.Series(dur1))

    df = pd.DataFrame(dict(ID = ids, DUR1 = date2, DUR2 = date1))
    df1 = pd.DataFrame(dict(ID = ids, Duration1 = date2, Duration2 = date1))
    df1['Duration1'] = df['DUR1'].dt.strftime('%H:%M:%S.%f')
    df1['Duration2'] = df['DUR2'].dt.strftime('%H:%M:%S.%f')
    cols = df.columns.tolist()
    cols = ['ID', 'DUR1', 'DUR2']
    df = df[cols]
    df['diff_seconds'] = df['DUR2'] - df['DUR1']
    df['diff_seconds'] = df['diff_seconds']/np.timedelta64(1,'s')
    df['TimeDelta'] = df['diff_seconds'].apply(lambda d: str(datetime.timedelta(seconds=abs(d))))
    df3 = df1.merge(df, on='ID')
    cols = df3.columns.tolist()
    cols = ['ID', 'Duration1', 'Duration2', 'TimeDelta', 'diff_seconds']
    df3 = df3[cols]
    print(df3)

The math is: Duration2-Duration1=TimeDelta

The function does it nicely:

Duration1        Duration2         TimeDelta           diff_seconds
00:00:23.999891  00:00:25.102076   0:00:01.102185      1.102185
00:00:43.079173  00:00:44.621481   0:00:01.542308      1.542308

But when Duration2 < Duration1 we have a negative diff_seconds, but TimeDelta is still positive:

Duration1        Duration2         TimeDelta           diff_seconds
00:05:03.744332  00:04:58.008081   0:00:05.736251     -5.736251

So what I need my function to do is to convert TimeDelta to negative value like this:

Duration1        Duration2         TimeDelta           diff_seconds
00:05:03.744332  00:04:58.008081   -0:00:05.736251     -5.736251

I suppose that I need to convert 'TimeDelta' in another way, but all my attempts were useless.

I'll be very thankful if somebody will help me with this.

Thanks in advance!

forest-man
  • 27
  • 2
  • 6
  • 2
    Possible duplicate of [Python timedelta issue with negative values](https://stackoverflow.com/questions/8408397/python-timedelta-issue-with-negative-values) – MyNameIsCaleb Oct 17 '19 at 13:47
  • Hi @MyNameIsCaleb , thanks for the hint, but I'm confused how can I apply those approaches to my current function. – forest-man Oct 17 '19 at 14:21

1 Answers1

0

I've solved this issue.

Made one by one timestamp picking logic and pass timestamps to 'time_convert' function

df['diff_seconds'] = df['DUR2'] - df['DUR1']
df['diff_seconds'] = df['diff_seconds']/np.timedelta64(1,'s')

for i in df['diff_seconds']:
    df['TimeDelta'] = time_convert(i)

And the time_convert function just appends "-" to formatted timestamp if the seconds were negative:

def time_convert(d):
    if d > 0:
        lst.append(str(datetime.timedelta(seconds=d)))
    else:
        lst.append('-' + str(datetime.timedelta(seconds=abs(d))))

And then, I've just created new data frame using lst, and merged all together

df_t = pd.DataFrame(dict(ALERTS = alerts, TimeDelta = lst))
df_f = df_t.merge(df3, on='ID')

Hope this will help somebody.

forest-man
  • 27
  • 2
  • 6