I have a datetime.time object as 02:00:00 i would like to convert it to the total seconds which should be 7200 seconds.
You can combine the time with a reference date to get a datetime object. If you then subtract that reference date, you get a timedelta object, from which you can take the total_seconds
:
from datetime import datetime, time
t = time(2,0,0)
ts = (datetime.combine(datetime.min, t) - datetime.min).total_seconds()
print(ts)
# 7200.0
With pandas
, I'd use the string representation of the time object column (Series) and convert it to timedelta datatype - which then allows you to use the dt
accessor to get the total seconds:
import pandas as pd
df = pd.DataFrame({'time': [time(2,0,0)]})
df['totalseconds'] = pd.to_timedelta(df['time'].astype(str)).dt.total_seconds()
# df['totalseconds']
# 0 7200.0
# Name: totalseconds, dtype: float64