简体   繁体   中英

Error converting date from DD/MM/YYYY HH.mm.ss to MM-DD-YYYY HH:MM:SS in python

I have a dataset which has a column "timestamp" in the Italian format: 11/03/2004 00.00.00 and I need to convert it as 2004-03-11 00:00:00 . I followed the guide on this old post but when I try to do this:

for index, row in dataset.iterrows():
   d = datetime.strptime(row['timestamp'], '%d/%m/%y %H.%M.%S')
   row['timestamp'] = d.strftime('%y-%m-%d %H:%M:%s')

I get the error:

ValueError: time data '11/03/2004 00.00.00' does not match format '%d/%m/%y %H.%M.%S'

How can I solve it? The format seems correct to me

You're using the wrong year format, according to the documentation you should be using %Y instead of %y . %y is for years without the century.

from datetime import datetime
datetime.strptime('11/03/2004 00.00.00', '%d/%m/%Y %H.%M.%S')

Refer strftime() and strptime() Format Codes

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