Pandas Dataframe date reformat

Viewed 22

I have a pandas dataframe (image supplied) where there is inconsistent dates, I need to add 20 after the second bracket, is there a formula I can apply to do this?

enter image description here

2 Answers

Use pd.to_datetime and dayfirst=True in order to remove any ambiguity: "13/08/16" can be interpreted both like "2016-08-13" or like "2013-08-16"

# df is eplData
df['date2'] = pd.to_datetime(df['date'], dayfirst=True).dt.strftime('%d/%m/%Y')
print(df)

# Output
         date       date2
0  13/05/2018  13/05/2018
1    13/08/16  13/08/2016

You should convert that column into a datetime column like this:

eplData['column_name'] = pd.to_datetime(eplData['column_name'])

There are also specific ways you can format the datetime column. More info here

Related