I have a database with a column named ['birth_date'], already converted string -> date using:
dataCopy.loc[:,'birth_date'] = dataCopy['birth_date'].astype('datetime64[ns]')
I also converted other columns my db has. So, as some of you know there is an issue with 2 digits year dates (mm/dd/yy or whatever) that when python sees a date like mm/dd/69 and below it assumes the year is 2069 and not 1969. Problem is I need to subtract this column with another column to pick the age my customer had when he canceled the service. Example: He was born in 1969 and canceled the service in 2019, so he was 53 years old. I already know how I can do it:
dataCopy['idade'] = (dataCopy['deleted_at'].dt.year - dataCopy['birth_date'].dt.year)
But first I need to fix the wrong years. Using format (y%m%d and variations) doesn't work. I mean, they work but they don't fix the wrong years. I am a beginner, already tried functions I saw here on Stack but I couldn't modify it to match my problem (plus I didn't understand it 100%). I appreciate any help.
You need to create a custom function and map it to the birth_date column.
You can decide a cutoff year (say 40) above which will be classified to 19th century and below which will be classified to 20th century. For example, 62 will be converted to year 1962, 32 will be converted to 2032.
The below code create the custom function that converts the datestring.
Once the custom function is created, you may use it on your birth_date column.
There is a possibility that
birth_datecolumn is already a date object. In that case you need to convert it to string before applying thecustom_date_function.