Paul Corcoran
Paul Corcoran

Reputation: 101

Pandas Dataframe date reformat

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

Upvotes: 1

Views: 25

Answers (2)

DataPlug
DataPlug

Reputation: 348

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

Upvotes: 0

Corralien
Corralien

Reputation: 120391

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

Upvotes: 1

Related Questions