I want to find the seconds difference between a datetime variable and a timedelta variable.
So my endtime variable is in timedelta format but the relevant information is 09:59:06.374829103
My starttime variable is in datetime format and the relevant information is 9:58
I want to find the difference between these two
df['endTime'][0]:
output: Timedelta('0 days 09:59:06.374829103')
df['startTime'][0]
output: datetime.datetime(2023, 1, 6, 9, 58)
I want how many seconds for:
(df['endTime'][0] - df['startTime'][0]) seconds
CodePudding user response:
You can use dt
accessor to convert DatetimeIndex
in total_seconds
:
# dt - dt.normalize() remove the date part and keep only time
>>> df['endTime'].dt.total_seconds() - \
df['startTime'].sub(df['startTime'].dt.normalize()).dt.total_seconds()
0 66.374829
dtype: float64
Input data:
>>> df
endTime startTime
0 0 days 09:59:06.374829103 2023-01-06 09:58:00
>>> df.dtypes
endTime timedelta64[ns]
startTime datetime64[ns]
dtype: object