简体   繁体   中英

How to fix an error converting a time column in a dataframe with the to_datetime?

I am have troubles converting a date column in my dataframe to ensure its a date type. Here is my dataframe:

    symbol   price    time
0   MSFT      97.71   9:39:02 AM
1   GOOG    1067.87   9:39:06 AM
2   AAPL     187.61   9:39:07 AM
3   CA        35.39   9:38:15 AM
4   SAP      110.76   9:38:13 AM

The thing is the time column already has AM/PM.

Here is my code:

dropped_col['time'] = pd.to_datetime(dropped_col.time,  format='%I:%M:%S')

The error that I get is:

ValueError: unconverted data remains: AM

Basically, I want the time field to be formatted correctly for charting and using any stat functions.

Is there something that I am missing?

You need:

dropped_col['time'] =pd.to_datetime(dropped_col.time.time,  format='%I:%M:%S %p')

Refer to docs for more information.

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