简体   繁体   中英

Pandas to time delta with only hours minutes and seconds

In my script i extract a excel that in one column called "Time" is dtype object, in that column there's a hour like so "14:00:00" i want to convert the column to_datetime but when i do this:

df['Time']=pandas.to_datetime(df['Time'],format='%H:%M:%S')

i adds the year month and day to it and i dont want that to happen and i also want to keep that column as a datetime so i can then subtract to another time and get the seconds. How can i pass it to datetime with only the hours minutes and seconds?

Use Series.dt.time :

df['Time']=pandas.to_datetime(df['Time'],format='%H:%M:%S').dt.time

For adding or subtracting time , you can do this:

pd.Timedelta(df.Time - t1).seconds / 3600.0

you can use timedelta :

df['Time'] = pd.to_timedelta(df['Time'])

To calculate minutes:

pd.to_timedelta(df.time).dt.seconds/60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM