'How to find the number of seconds elapsed from the start of the day in pandas dataframe

I have a pandas dataframe df in which I have a column named time_column which consists of timestamp objects. I want to calculate the number of seconds elapsed from the start of the day i.e from 00:00:00 Hrs for each timestamp. How can that be done?

enter image description here



Solution 1:[1]

You can use pandas.Series.dt.total_seconds

df['time_column'] = pd.to_datetime(df['time_column'])
df['second'] = pd.to_timedelta(df['time_column'].dt.time.astype(str)).dt.total_seconds()

Solution 2:[2]

Do df['time_column]. That will give you the time column. Than just do something like:

 import datetime as date
 current_date = date.datetime.now()
 time_elapsed = []
 for x in range(0, current_date.minute*60 + current_date.hour*60*60):
     time_elapsed.append((df['time_column'][x].minute*60 + df['time_column][x].hour*60*60)- (current_date.minute*60 + current_date.hour*60*60))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ynjxsjmh
Solution 2 Poder Psittacus