This is my data frame
df = pd.DataFrame({
'Date': ['01/05/2015', '15 Jul 2009', '21.03.12','2021.03.12']
})
Date
0 01/05/2015
1 15 Jul 2009
2 21.03.12
3 2021.03.12
I want all the dates from 'Date' column in yyyy-mm-dd format. The expected output is given below
Date
0 2015-01-05
1 2009-07-15
2 2021-03-12
3 2021-03-12
But i am getting
Date
0 2015-01-05
1 2009-07-15
2 2012-03-21
3 2021-03-12
Here, the format 21.03.12 is not recognized as yy.mm.dd .To rectify this I added '20' infront of yy.mm.dd format dates, this works but not a permanent solution. Is there any better solution for this?
Thanks in advance!
CodePudding user response:
I think you're after this:
import pandas as pd
df = pd.DataFrame({
'Date': ['01/05/2015', '15 Jul 2009', '21.03.12', '2021.03.12']
})
df['Current'] = pd.to_datetime(df['Date'])
df['Desired'] = pd.to_datetime(df['Date'], yearfirst=True)
print(df)
Result:
Date Current Desired
0 01/05/2015 2015-01-05 2015-01-05
1 15 Jul 2009 2009-07-15 2009-07-15
2 21.03.12 2012-03-21 2021-03-12
3 2021.03.12 2021-03-12 2021-03-12
In 'Desired'
, note how 21
is now interpreted as the year.