I have a pandas dataframe which has a column 'dob' (date of birth), I wish to compute the age based on today's date
I have used datetime module to invoke today, and subtract the 'dob' field from today, and divide this by 365 to get the age in years.
This is a rather crude approach I concede, I am looking for hints to do it more elegantly.
# -*- coding: utf-8 -*-
import pandas as pd
from datetime import datetime
today = datetime.today()
df = pd.read_csv(pathtocsvfile, parse_dates=['dob'])
df['age'] = df['dob'].apply(lambda x: (today - x).days // 365)
I believe the code is working as it is, however I am not sure how much leap years can influence the result.
And I am looking for an elegant way to do this as well.