mad_
mad_

Reputation: 8273

Converting two date formats in one format pandas

What would be the easiest way to transform two date formats in pandas dataframe into one? Below is my input

import pandas as pd
df = pd.DataFrame({'DOB': {0: '26/1/16 ', 1: '26/1/2016 '}})

Expected output:

       DOB
0   26/1/2016
1   26/1/2016

I need to maintain the same dateformat.

Followed the comments and used pd.to_datetime

pd.to_datetime(df.DOB)

Output:

0   2016-01-26
1   2016-01-26

Which is fine but I want to retain the original format of %d/%m/%Y

I have tried using the format argument after executing pd.to_datetime

df.DOB=pd.to_datetime(df.DOB,format="%d/%m/%Y")

It seems it does not changes the format. Output:

0   2016-01-26
1   2016-01-26

If I directly execute below to provide the format it gives error which makes sense as input is not of same format

df = pd.DataFrame({'DOB': {0: '26/1/2016 ', 1: '26/1/2016 '}})
df.DOB=pd.to_datetime(df.DOB,format="%d/%m/%Y")

ERROR:

ValueError: unconverted data remains:  

Long story short how can I retain the original date format

Upvotes: 0

Views: 999

Answers (1)

jpp
jpp

Reputation: 164613

You have whitespace in your date strings. Use pd.Series.str.strip before calling pd.to_datetime to guarantee you will avoid errors.

Option 1: don't specify format

Here you don't even need to strip whitespace:

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

df.DOB = pd.to_datetime(df.DOB)

print(df)

         DOB
0 2016-01-26
1 2016-01-26

Option 2: strip before specifying format

Supply format only if you know beforehand the specific format of your dates. If you do this, you will need to use pd.Series.str.strip beforehand:

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

df.DOB = pd.to_datetime(df.DOB.str.strip(), format='%d/%m/%Y')

print(df)

         DOB
0 2016-01-26
1 2016-01-26

Upvotes: 1

Related Questions