4

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?

Nithin Das
  • 364
  • 1
  • 5
  • 14
  • Possible duplicate of [How to parse string dates with 2-digit year?](https://stackoverflow.com/questions/16600548/how-to-parse-string-dates-with-2-digit-year) – Psytho Dec 18 '17 at 10:12
  • https://stackoverflow.com/questions/40422517/convert-date-from-dd-mm-yy-to-dd-mm-yyyy-using-python – Psytho Dec 18 '17 at 10:13
  • @Alex.S there are (almost always) better/different solutions for datetime operations when it comes to pandas. – cs95 Dec 18 '17 at 10:35

1 Answers1

5

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252