pandas timedelta to seconds
df['column_with_timedelta'].dt.total_seconds()
pandas timedelta to seconds
df['column_with_timedelta'].dt.total_seconds()
python timedelta
from datetime import timedelta
# Create a delta time
datetime_delta = timedelta(weeks = 1, days = 2, hours = 4, minutes = 10,
seconds = 8, milliseconds = 25, microseconds = 8)
print("Delta datetime :- ", datetime_delta)
>>> Delta datetime :- 9 days, 4:10:08.025008
converting pandas._libs.tslibs.timedeltas.Timedelta to days
# Convert TimeDelta column (5 days etc.) into an integer column (5)
import numpy as np
df.timeDelta = (df.timeDelta / np.timedelta64(1,'D')).astype(int)
pd.timedelta
# convert the entire timedelta to seconds
# this is the same as td / timedelta(seconds=1)
(df.from_date - df.to_date).dt.total_seconds()
[out]:
0 211089.82
1 13264.30
2 31373.76
dtype: float64
# get the number of days
(df.from_date - df.to_date).dt.days
[out]:
0 2
1 0
2 0
dtype: int64
# get the seconds for hours + minutes + seconds, but not days
# note the difference from total_seconds
(df.from_date - df.to_date).dt.seconds
[out]:
0 38289
1 13264
2 31373
dtype: int64
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us