0

So I have a dataframe that has the date of birth written as a string '2012-07-06T21:00:00.000Z' (example).

I want to calculate the users' age from that. Note that this is a string and I think I need it to be datetime to be able to calculate it.

Thank you much

my code (Python):

def calculate_age(born):
    born = datetime.strptime(born, "%Y-%m-%d'T'%H:%M:%S.%f%Z").date()
    today = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

df['age'] = df['birth_date'].apply(calculate_age) 
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • Have a look at the library ```dateparser```. this can help if you dont have a predefined format – Yash Nov 01 '20 at 07:11

1 Answers1

0

What about:

import pandas as pd

df['birth_date'] = df['birth_date'].astype('datetime64[ns, UTC]')
df['age'] = pd.Timestamp.utcnow() - df['birth_date']
df['age_str'] = df['age'].apply(lambda td: f'{td.days//365}y{int((td.days/365 - td.days//365)*365/30)}m')

Here, you first convert the birth_date column to datetime dtype, then you calculate the time delta using current time. Finally you calculate from days the number of years and month.

Stefan
  • 1,697
  • 15
  • 31