I'm trying to subtract column df['date_of_admission']
from the column df['DOB']
to find the difference between then and store the age value in df['age']
column, however, I'm getting this error:
OverflowError: Overflow in int64 addition
DOB date_of_admission age
2000-05-07 2019-01-19 12:26:00
1965-01-30 2019-03-21 02:23:12
NaT 2018-11-02 18:30:10
1981-05-01 2019-05-08 12:26:00
1957-01-10 2018-12-31 04:01:15
1968-07-14 2019-01-28 15:05:09
NaT 2018-04-13 06:20:01
NaT 2019-02-15 01:01:57
2001-02-10 2019-03-21 08:22:00
1990-03-29 2018-11-29 03:05:03
..... ......
..... .....
..... .....
I've tried it with the following:
import numpy as np
import pandas as pd
from datetime import dt
df['age'] = (df['date_of_admission'] - df['DOB']).dt.days // 365
Expected to get the following age column after finding the difference between:
age
26
69
NaN
58
.
.
.
Convert both columns into date then subtract it
import pandas as pd
df['date_of_admission'] = pd.to_datetime(df['date_of_admission']).dt.date
df['DOB'] = pd.to_datetime(df['DOB']).dt.date
df['age'] = ((df['date_of_admission']-df['DOB']).dt.days) //365
SECOND TEST
#Now I have use DOB AND date_of_admission data from the question and it is working fine
df = pd.DataFrame(data={"DOB":['2000-05-07','1965-01-30','NaT'],
"date_of_admission":["2019-01-19 12:26:00","2019-03-21 02:23:12", "2018-11-02 18:30:10"]})
df['DOB'] = pd.to_datetime(df['DOB']).dt.date
df['date_of_admission'] = pd.to_datetime(df['date_of_admission']).dt.date
df['age'] = ((df['date_of_admission']-df['DOB']).dt.days) //365
RESULT:
DOB date_of_admission age
2000-05-07 2019-01-19 18.0
1965-01-30 2019-03-21 54.0
NaT 2018-11-02 NaN