andrecdazevedo
andrecdazevedo

Reputation: 43

How to get an age column through date of birth in pandas?

Need to get the age from a datetime64[ns] date of birth column.

Tried:

now = pd.Timestamp(DT.datetime.now())
clients["age"] = ((now - clients["date_of_birth"])/365)   
clients["age"].astype(int)

But following error appeared:

TypeError: cannot astype a timedelta from [timedelta64[ns]] to [int32]

clients["age"]
0                            NaT
1     22624 days 11:35:15.057887
2     18579 days 11:35:15.057887
3     14918 days 11:35:15.057887
4     21680 days 11:35:15.057887
5     11090 days 11:35:15.057887
6     19346 days 11:35:15.057887
7     11535 days 11:35:15.057887
8     12224 days 11:35:15.057887
9     11431 days 11:35:15.057887

Upvotes: 1

Views: 1312

Answers (1)

Rakesh
Rakesh

Reputation: 82765

Looks like you need.

clients["age"].dt.days

Upvotes: 1

Related Questions