Home > Software engineering >  How to convert birthday to age using datetime, but on an entire pandas series?
How to convert birthday to age using datetime, but on an entire pandas series?

Time:01-25

I have the following code:

def birthdayToAge(birthday, reference):
    return reference.year - birthday.year - ((reference.month, reference.day) < (birthday.month, birthday.day))

This works on individual values, but generates the error The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). when applied to two series. The cause of this error is the "<".

How do I change the ("<" in the) function to not get this error?

CodePudding user response:

I hope you are converting series to datetime. df['birthday'] = pd.to_datetime(df['birthday'])

Define a function to calculate age

df['birthday'] = pd.to_datetime(df['birthday'])
def calculate_age(birthday):
    today = datetime.now()
    age = today.year - birthday.year - ((today.month, today.day) <(birthday.month, birthday.day))
    return age

CodePudding user response:

You could try this, using the numpy timedelta to get the distance in years from the reference instead of calculating it yourself.

import pandas as pd
import datetime
import random
import numpy as np

def birthdayToAge(birthday,reference):
    return np.floor((reference-birthday)/np.timedelta64(1,"Y"))

data = {"birthday":[datetime.datetime.now()- datetime.timedelta(days=random.randint(800,800*10)) for x in range(10)],
        "randomdata":[random.randint(1,5) for x in range(10)]}

df = pd.DataFrame(data)

df["age"] = birthdayToAge(df["birthday"],datetime.datetime.now())

print(df)
  • Related