0

I read several topics and could not find a solution. I need to find out the age of the person and I currently have the following.

Column func ['dtNasc'], type str Variable dateToCompare, type datetime.datetime

I know I need to do func ['dtNasc'] - dateToCompare, to generate a result of the timedelta type, and only then can do:

func['idade'] = [[func['dtNasc'] - dateToCompare]/365.25]

How do I convert the func ['dtNasc'] column to datetime?

  • Are you looking for `pd.to_datetime(func['dtNasc'])`? You can also try this: `func['idade']= (pd.to_datetime(func['dtNasc']) - dateToCompare).astype(' – Anton vBR Jun 29 '18 at 17:09
  • if `[func['dtNasc']` is a string you will need to convert it into a meaningful thing... if it is a string of number of days old someone is... then you could probably just do `int([func['dtNasc'])` if you want to convert to seconds like a time.time() you would multiply by 3600*24 – Grady Player Jun 29 '18 at 17:10
  • Possible duplicate of [Convert Pandas Column to DateTime](https://stackoverflow.com/questions/26763344/convert-pandas-column-to-datetime). In case this is what you are looking for. You can also look here: https://stackoverflow.com/questions/26788854/pandas-get-the-age-from-a-date-example-date-of-birth – Anton vBR Jun 29 '18 at 17:13
  • When I do func['dtNasc'] = func['dtNasc'].apply(lambda x: datetime.strptime(x, '%d%b%Y:%H:%M:%S.%f') ) I get the error "timedate 18/11/1978 does not mach format '%d%b%Y:%H:%M:%S.%f'" – Brenda Xavier Jun 29 '18 at 17:39
  • when I do func['idade']= (pd.to_datetime(func['dtNasc']) - dateToCompare).astype(' – Brenda Xavier Jun 29 '18 at 17:46
  • @BrendaXavier Your format is `%d/%m/%Y`. I think it is time to share data! – Anton vBR Jun 29 '18 at 19:03

1 Answers1

0

I was able to solve it like this:

func['idade'] = pd.to_datetime(func['dtNasc'])
func['idade'] = (dateToCompare - func['idade'])
func['idade'] = (func['idade']/365.25)
func['final'] = fun['idade'].astype('timedelta64[D]')
func['final'] = func['final'].astype(int)