r0xette r0xette - 29 days ago 15
Python Question

Convert date format to insert into mysql db

I have pandas column

row['date']
which contains date in format
11/05/2015
. I am trying to insert it into mysql db but having problems due to incorrect format of date field data. It has to be converted into
2015-11-05
in order to be inserted. Without storing the new value in variable how can I convert the date into required format?

Current format: 11/05/2015
Required format: 2015-11-05

Mat Mat
Answer

Is the current format mm/dd/yyyy? If so

from datetime import datetime
row['date'] = datetime.strptime(row['date'], '%m/%d/%Y').strftime('%Y-%m-%d')