Reputation: 312
Hi Guys i need to calculate delta between 2 datetime64[ns]
However this are returning to me a biggest delta, thata is visible unreal. I donr understanding the reson why this error are happening, because both dates have the same format.
df1=
entrada First_Time_log
0 2020-06-09 01:50:00 2020-06-09 03:13:22
1 2020-06-10 01:50:00 2020-06-10 02:31:31
2 2020-06-11 01:50:00 2020-06-11 02:00:07
3 2020-06-12 01:50:00 2020-06-12 03:39:59
4 2020-06-13 01:50:00 2020-06-13 04:05:28
... ... ...
4255 2020-06-02 01:50:00 2020-06-02 02:00:02
4256 2020-06-03 01:50:00 2020-06-03 02:09:16
4257 2020-06-04 01:50:00 2020-06-04 01:20:14
4258 2020-06-05 01:50:00 2020-06-05 01:11:39
4259 2020-06-06 01:50:00 2020-06-06 01:35:11
But after apply operation below:
dfc['entrada-first'] = dfc['entrada'] - dfc['First_Time_log']
It returns biggest range, so the range bitwenn dates is less then 24 hours. What i doing wrong?
entrada First_Time_log entrada-first
0 2020-06-09 01:50:00 2020-06-09 03:13:22 -1 days +22:36:38
1 2020-06-10 01:50:00 2020-06-10 02:31:31 -1 days +23:18:29
2 2020-06-11 01:50:00 2020-06-11 02:00:07 -1 days +23:49:53
3 2020-06-12 01:50:00 2020-06-12 03:39:59 -1 days +22:10:01
4 2020-06-13 01:50:00 2020-06-13 04:05:28 -1 days +21:44:32
... ... ... ...
4255 2020-06-02 01:50:00 2020-06-02 02:00:02 -1 days +23:49:58
4256 2020-06-03 01:50:00 2020-06-03 02:09:16 -1 days +23:40:44
4257 2020-06-04 01:50:00 2020-06-04 01:20:14 00:29:46
4258 2020-06-05 01:50:00 2020-06-05 01:11:39 00:38:21
4259 2020-06-06 01:50:00 2020-06-06 01:35:11 00:14:49
Upvotes: 0
Views: 84
Reputation: 25544
it seems you want the absolute value of the timedelta? E.g.
import pandas as pd
# example df:
df = pd.DataFrame({'entrada': pd.to_datetime(['2020-06-03 01:50:00','2020-06-04 01:50:00']),
'First_Time_log': pd.to_datetime(['2020-06-03 02:09:16','2020-06-04 01:20:14'])})
# Python's built-in abs works fine here:
df['td_abs'] = abs(df['entrada']-df['First_Time_log'])
# df['td_abs']
# 0 00:19:16
# 1 00:29:46
# Name: td_abs, dtype: timedelta64[ns]
Upvotes: 2
Reputation: 1531
Try calculating the time difference as follows:
def find_time_difference(x):
if x["entrada"] >= x["First_Time_log"]:
return x["entrada"] - x["First_Time_log"]
elif x["entrada"] <= x["First_Time_log"]:
return x["First_Time_log"] - x["entrada"]
return ''
df["entrada-first"] = df.apply(lambda x: find_time_difference(x), axis=1)
output:
Upvotes: 1