对于已经是datetime格式的 x1
x1 = pd.to_datetime( market.DATE, format=”%Y-%m-%d-%H-%M-%S”)
x1 = x1.dt.strftime(‘%m/%d/%Y’)
source:https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas
You can use dt.strftime, if need convert datetime to other formats, but then dtype of column is 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