简体   繁体   中英

python convert timestamp without year-month-day

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!

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

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