-2

I have a column, which is datetime type. There are null values in it. The date is stored in it in the form of 'yyyy/mm/dd' i.e 2016-01-22 and I want to change in 'mm/dd/yyyy' i.e. is 01-22-2016.

pd.to_datetime(FinalDF['P-IN.Date'],errors='coerce',format="%Y/%m/%d")

The above code is what I tried. Can anyone help me out ? Thanks and Regards.

  • Duplicate of [https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – Arman Jan 24 '22 at 12:47
  • 1
    Does this answer your question? [How to change the datetime format in Pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – Vhndaree Jan 24 '22 at 12:49

1 Answers1

0

You can use dt.strftime() to convert the date format. Here is an example

import pandas as pd

df = pd.DataFrame({'DOB': {0: '26/1/2016', 1: '26/1/2016'}})
print (df)
         DOB
0  26/1/2016 
1  26/1/2016

df['DOB'] = pd.to_datetime(df.DOB)
print (df)
         DOB
0 2016-01-26
1 2016-01-26

df['DOB1'] = df['DOB'].dt.strftime('%m/%d/%Y')
print (df)
         DOB        DOB1
0 2016-01-26  01/26/2016
1 2016-01-26  01/26/2016
Emilio
  • 102
  • 11