Reputation: 153
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?
Upvotes: 1
Views: 5623
Reputation: 34086
Use Series.dt.time
:
import pandas as pd
# sample
df = pd.DataFrame({'Time': ['00:00:05', '00:00:10', '00:10:00']})
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S').dt.time
print(type(df.Time[0]))
[out]:
<class 'datetime.time'>
For adding or subtracting time
, you can do this:
pd.Timedelta(df.Time - t1).seconds / 3600.0
Upvotes: 4
Reputation: 13349
you can use timedelta
:
df['Time'] = pd.to_timedelta(df['Time'])
To calculate minutes:
pd.to_timedelta(df.time).dt.seconds/60
Upvotes: 1