简体   繁体   中英

Python/Pandas Datetime Conversion of Date in Format DD-MON-YY HH:MM:SS.NS PM

Running into some issues with datetime conversion.

import pandas as pd
mydate = '12-AUG-03 04.16.41.000000 PM'
mydateconv = pd.to_datetime(mydate)
print mydateconv
"2003-08-12 12:00:00"

Is there a reason the time is being reset to 12:00:00?

I've also tried other formatting derivations without success.

mydateconv = pd.to_datetime(mydate, format = '%d-%m-%y %I:%M:%S.%f %p')

Any recommendations?

The raw data I'm receiving has dates in the above format, so I'm looking for suggestions on a solution which addresses dates in this format, whether it's the use of stock function or determination that I'll need something a bit more custom because of the format.

Many thanks in advance for any thoughts.

Your format string needs to be: '%d-%b-%y %I.%M.%S.%f %p' , see the docs :

In [35]:

pd.to_datetime('12-AUG-03 04.16.41.000000 PM', format = '%d-%b-%y %I.%M.%S.%f %p')
Out[35]:
Timestamp('2003-08-12 16:16:41')

You had several errors in your format string '%d-%m-%y %I:%M:%S.%f %p' .

Firstly your months are abbreviated so you should use b instead of m .

Secondly your time components had dot ( . )separators not colon ( : ) separators.

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