python convert timestamp without year-month-day

Viewed 691

I have a dataframe with time column as string and I should convert it to a timestamp only with h:m:sec.ms . Here an example:

import pandas as pd
df=pd.DataFrame({'time': ['02:21:18.110']})
df.time= pd.to_datetime(df.time , format="%H:%M:%S.%f")
df # I get 1900-01-01 02:21:18.110

Without format flag, I get current day 2020-12-16. How can I get the stamp without year-month-day which seemingly always is included. Thanks!

2 Answers

You need this:

df=pd.DataFrame({'time': ['02:21:18.110']})
df['time'] = pd.to_datetime(df['time']).dt.time

In [1023]: df
Out[1023]: 
              time
0  02:21:18.110000

If need processing values later by some datetimelike methods better is convert values to timedeltas by to_timedelta instead times:

df['time'] = pd.to_timedelta(df['time'])
print (df)
                    time
0 0 days 02:21:18.110000
Related