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))
I think problem is your problematic values are converted from
None
toNaN
, soint
is cast tofloat
- see docs.Instead
map
you can useto_numeric
with parametererrors='coerce'
for convert problematic values toNaN
: