Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 420
physiker Avatar asked Jun 08 '26 21:06

physiker


2 Answers

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
like image 105
jezrael Avatar answered Jun 10 '26 10:06

jezrael


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
like image 23
Mayank Porwal Avatar answered Jun 10 '26 11:06

Mayank Porwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!