Reputation: 2871
I have a dataframe as shown below
Name Birth_Year
Messi 2007
Ronaldo 2004
I would like to convert that Birth_Year into date.
Expected Output
Name Birt_Year Birth_Date
Messi 2007.0 2007-01-01
Ronaldo 2004.0 2004-01-01
I tried below code
df1['Year'] = pd.to_datetime(df1['Birt_Year'], format='%Y%m%d.0', errors='coerce')
Upvotes: 0
Views: 765
Reputation: 42916
You were almost there, your format is just %Y
, since you only have year on your Birth_Year
column:
df['Birth_Date'] = pd.to_datetime(df['Birth_Year'], format='%Y')
Name Birth_Year Birth_Date
0 Messi 2007 2007-01-01
1 Ronaldo 2004 2004-01-01
Upvotes: 3