Problem converting time into pandas datetime

1k Views Asked by At

I am trying to convert a date column containing only hours, minutes and seconds ito a datetime form using pandas.to_datetime(). However, it adds year and date automatically. I also tried using pandas.to_datetime(df["time"], format = %H:%M:%S").dt.time, again the data type remains object. Is there any method that can change into datetime format without year and date?

2

There are 2 best solutions below

1
mrbc42 On

Something like this?

df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S', errors='ignore')
0
mrbc42 On

put .dt.time on the end

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