How do I get an age in years and date on pandas
Asked Answered
G

5

5

Here's my data

Customer_id    Date-of-birth
1              1992-07-02
2              1991-07-03

Here's my code

import datetime as dt
df['now'] = dt.datetime.now()
df['age'] = df['now'].dt.date - df['Date-of-birth']

Here's the result

Customer_id    Date-of-birth     age
1              1992-07-02        xxxx days
2              1991-07-03        xxxx days

The result that I want is

Customer_id    Date-of-birth     age
1              1992-07-02        26 years 22 days
2              1991-07-03        27 years 21 days

Just let you now, by df.dtypes, Date-of-birth is an object because is based on customer input in dropdown

How can I achieve this? I hope the question is clear enough

Gielgud answered 24/7, 2018 at 6:31 Comment(0)
R
7

Use this solution with custom function, because count it is not easy because leaps years:

from dateutil.relativedelta import relativedelta

def f(end):
    r = relativedelta(pd.to_datetime('now'), end) 
    return '{} years {} days'.format(r.years, r.days)

df['age'] = df["Date-of-birth"].apply(f)
print (df)
   Customer_id Date-of-birth               age
0            1    1992-07-02  26 years 22 days
1            2    1991-07-03  27 years 21 days
Reims answered 24/7, 2018 at 7:21 Comment(0)
P
5

Input:

import pandas as pd
import datetime as dt

now = dt.datetime.now()
for i in range(0, len(df)):
    diff = now - dt.datetime.strptime(df['Date-of-Birth'][i], '%Y-%m-%d')
    years = diff.days // 365
    days = diff.days - (years*365)
    df['age'][i] = str(years) + ' years ' + str(days) + ' days'

print(df)

Output:

Customer_id     Date-of-Birth          age
    1            1992-07-04       26 years 25 days
    2            1991-07-04       27 years 26 days
Presidio answered 24/7, 2018 at 6:58 Comment(0)
C
4

Maybe you could use something like the following. Note that it relies on the fact that the average year has 365.25 days, so it may be a day off sometimes.

import datetime as dt

def year_days_diff(x):
    diff = (dt.datetime.now() - x).days
    return str(int(diff / 365.25)) + ' years ' + str(int(diff / 365.25 % 1 * 365.25)) + ' days'

Example:

birth_date = dt.datetime.now() - dt.timedelta(10000)
year_days_diff(birth_date)

output:

'27 years 138 days'
Colner answered 24/7, 2018 at 6:52 Comment(2)
@NabihBawazir just make a for loop using df['age'][i] = year_days_diff(df['Date-of-Birth'][i]), or see/use Dahlia's answer ;)Colner
Chage the amount of days in a year is by 365.25 first> any method by only using year functionGielgud
J
2

This could give you age by rounding to years.

ref_date = dt.datetime.now()
df['age'] = df['Date-of-birth'].apply(lambda x: len(pd.date_range(start = x, end = ref_date, freq = 'Y'))) 
Jillianjillie answered 24/7, 2018 at 6:55 Comment(1)
will try this firstGielgud
Z
0

Use astype('<m8[Y]')

Ex:

df['age'] = (pd.to_datetime('now') - df['Date-of-birth']).astype('<m8[Y]')

Demo:

import pandas as pd

df = pd.DataFrame({"Date-of-birth": pd.to_datetime(['1992-07-24', '1991-07-24'])})
df["age"] = (pd.to_datetime('now') - df["Date-of-birth"]).astype('<m8[Y]')
print(df)

Output:

  Date-of-birth   age
0    1992-07-24  25.0
1    1991-07-24  27.0
Zeller answered 24/7, 2018 at 6:40 Comment(3)
Error: TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('<M8[ns]')Backspace
Do you have string datatype in your dates?Zeller
For me its work, but why 1992-07-24 give 25 years, it should be 26 years. Upvoted, it helps. but still waiting better answer that give years and dateGielgud

© 2022 - 2024 — McMap. All rights reserved.