I have a python pandas data frame that has a date column like below:
'Birth Date'
0 22/04/73
1 22/03/53
2 22/04/73
3 14/08/77
4 08/05/50
I want to convert this object datatype to date format, something like this: 04/06/1953...
I tried to convert to datetime datatype as below:
df['Birth Date']=pandas.to_datetime(df['Birth Date'],format='%d/%m/%y')
but the output was like:
0 1973-04-22
1 2053-03-22
2 1973-04-22
3 1977-08-14
4 2050-05-08
How do I get 1953, 1950 etc as the year instead of 2053, 2050?
You need manually change years by mask, e.g. all years more as 2017
are subtracted by 100
years:
df['Birth Date']= pd.to_datetime(df['Birth Date'],format='%d/%m/%y')
df['Birth Date'] = df['Birth Date'].mask(df['Birth Date'].dt.year > 2017,
df['Birth Date'] - pd.offsets.DateOffset(years=100))
print (df)
Birth Date
0 1973-04-22
1 1953-03-22
2 1973-04-22
3 1977-08-14
4 1950-05-08