How to change the datetime format in pandas

DOB column sample value is in the format - 1/1/2016 which by default gets converted to object as shown below

    DOB       object

Converting to date format

    df['DOB'] = pd.to_datetime(df['DOB'])

Date converts to

    2016-01-26

dtype is

    DOB       datetime64[ns]

Now I want to convert this date format to 01/26/2016 or in any other general date formats. How do I do it?

Whatever the method I try it always shows the date in 2016-01-26 format.

You can use dt.strftime if you need to convert datetime to other formats (but note that then dtype of column will be object (string)):

    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

From: stackoverflow.com/q/38067704