简体   繁体   中英

Python date format Convert to datetime

I have df as below, however the format is object. I am trying to convert to date format in python.

Shift End
2/9/2001 6:30:00 PM
2/10/2001 6:30:00 PM
2/11/2001 6:30:00 PM
2/12/2001 6:30:00 PM
2/13/2001 6:30:00 PM
2/8/2001 12:30:00 PM

I ma trying to use use below, but I get error time data '2/9/2001 10:00:00 AM' does not match format '%m/%d/%y %I.%M.%S.%f %p'

df["Shift Start "].apply(lambda x: datetime.strptime(x, '%m/%d/%y %I.%M.%S.%f %p'))

Use to_datetime with change format for matching your data - change %Y for YYYY, remove microseconds %f and change . to : :

df["Shift Start "] = pd.to_datetime(df["Shift Start "], format='%m/%d/%Y %I:%M:%S %p')

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