I want to convert a column to integer but the problem is that the column contains a missing value. The column converts to float fine, but cant convert to integer.
Sample code:
d2 = {'location': ['NY', 'NY', 'PA', 'NY', 'PA', 'PA', 'NY'], 'dep_name': ['hr', 'mk', 'fin', 'fin', 'hr', 'fin', 'fin'], 'Duration_of_Employment' : [10, 5, 9, 8, 2, 4, 7], 'Salary' : [50000, 86000,25000, 73000, 28000, 60000, 40000], 'Days_Since_Last_Promotion': ['61', '35', '25', '98', 'NaN', '45', '22']}
df2 = pd.DataFrame(data = d2)
df2['xy'] = df2['Days_Since_Last_Promotion'].astype(float)
df2['Months_Since_Last_Promotion'] = df2['xy'] // 30
Now 'Months_Since_Last_Promotion' is float type. But when I try to convert it to integer I get the following error.
df2['Months_Since_Last_Promotion'] = df2['Months_Since_Last_Promotion'].astype(int)
ValueError: Cannot convert NA to integer
From the error, I figured its due to the missing value Nan and tried this work around .But it didnt work and 'Months_Since_Last_Promotion' is still showing as float64.
df2.loc[df2['Months_Since_Last_Promotion'].notnull(), 'Months_Since_Last_Promotion'] = df2.loc[df2['Months_Since_Last_Promotion'].notnull(), 'Months_Since_Last_Promotion'].astype(int)
Note: I cant use fillna to replace the NaN. The goal is to keep the column as integer.