[Pandas]How to calculate datetime difference in years

Suppose you got a person’s regist time in one column and his birth date in another column, now you need to calculate his age when he did the registration. There are two ways to reach this result.

  1. With the help of relativedelta
1
2
3
from dateutil import relativedelta
for i in df.index:
df.loc[i, 'age'] = relativedelta.relativedelta(df.loc[i, 'regDate'], df.loc[i, 'DateOfBirth']).years
  1. With the help of np.timedelta64
1
2
3
4
5
import numpy as np

df['age'] = (df['regDate'] - df['DateOfBirth'])/np.timedelta64(1, 'Y')
# You may need to convert to integer
df['age'].apply(np.int64)
0%