Reputation: 77
So I'm trying to calculate the difference between the date now with the DOB and came up with a column 'Age'
My codes are
df = df.rename(columns={'dob Y':'Year','dob M':'Month','dob D':'Day'})
df = df.head(13)
import datetime
#df['DOB'] = pd.to_datetime(df[['Day','Month','Year']])
df['Age'] = (datetime.datetime.now()-pd.to_datetime(df[['Day','Month','Year']]))
df['Age']
# [i.days for i in df['Age']]
However I've gotten the age in days instead
0 3381 days 01:44:57.149175
1 3513 days 01:44:57.149175
2 3132 days 01:44:57.149175
3 3740 days 01:44:57.149175
4 4551 days 01:44:57.149175
5 3205 days 01:44:57.149175
6 3814 days 01:44:57.149175
7 5820 days 01:44:57.149175
8 4533 days 01:44:57.149175
9 2445 days 01:44:57.149175
10 2665 days 01:44:57.149175
11 5528 days 01:44:57.149175
12 3707 days 01:44:57.149175
Name: Age, dtype: timedelta64[ns]
I've tried to divide by 365.25 but the unit given is still days (as shown below)
0 9 days 06:09:54.223747
1 9 days 14:50:18.864404
2 8 days 13:48:13.197053
3 10 days 05:45:15.784322
4 12 days 11:02:38.084117
5 8 days 18:36:01.369537
6 10 days 10:37:00.507114
7 15 days 22:25:40.424979
8 12 days 09:51:40.178572
9 6 days 16:39:43.135451
10 7 days 07:07:04.203213
11 15 days 03:14:27.735041
12 10 days 03:35:09.624158
Name: Age, dtype: timedelta64[ns]
I'm trying to round off the Age to years in 2 decimal place. Example 12.96 years
Upvotes: 0
Views: 1505
Reputation: 140
Due to issues explained in this post, it is impossible to get the exact number of years from a number of days due to variations in year length.
If you want to use 365.25 (or 365.2425, as the linked post suggests) as the number of days in a year, you could do something like this:
df['Age'] = pd.to_numeric((datetime.datetime.now()-pd.to_datetime(df[['Day','Month','Year']])).days / 365.2425)
Including the int() cast so that we don't get a long decimal.
Upvotes: 1