I am trying to change a column's data type from type: object to type: int64 within a DataFrame using .map().
df['one'] = df['one'].map(convert_to_int_with_error)
Here is my function:
def convert_to_int_with_error(x):
if not x in ['', None, ' ']:
try:
return np.int64(x)
except ValueError as e:
print(e)
return None
else:
return None
if not type(x) == np.int64():
print("Not int64")
sys.exit()
This completes successfully. However, when I check the data type after completion, it reverts to type: float:
print("%s is a %s after converting" % (key, df['one'].dtype))
if not type(x) == np.int64():condition? Are you saying thatconvert_to_int_with_errornever returnsNone?Nonewill be regarded asNaNso as to keep it'sfloat(numerical) dtype. You need to find a way to handle such missing values/empty strings so that it would result innp.int64dtype.