简体   繁体   中英

Converting a dtype: object to datetime not converting

I am trying to convert the df['start_time_time'] column from object to datetime.

start_time start_time_time
2019-01-01 00:04:37 00:04:37
2019-01-01 00:14:52 00:14:52

The df['start_time_time'] column was extracted from the df['start_time'] column.

I tried converting using this:

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

But the data type is still shown as an object.

I also tried this:

pd.to_datetime(df['start_time_time'], format='%H:%M:%S')

Which successfully converts it to datetime64, but the results include the (wrong) date as seen below:

start_time start_time_time
2019-01-01 00:04:37 1900-01-01 00:04:37
2019-01-01 00:14:52 1900-01-01 00:14:52

Does the datetime format have to include a date?

I am looking for a way to organize/sort the rows by time regardless of date.

Thank you!

Try using this:

df['start_time_time'] = pd.to_datetime(df['start_time_time']).dt.normalize()

It will convert your date to 'datetime64' datatype

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