How can I resolve – TypeError: cannot safely cast non-equivalent float64 to int64?

Question:

I’m trying to convert a few float columns to int in a DF but I’m getting above error. I’ve tried both to convert it as well as to fillna to 0(which I prefer not to do, as in my dataset the NA is required).

What am I doing wrong?
I’ve tried both:

orginalData[NumericColumns] = orginalData[NumericColumns].astype('Int64')
#orginalData[NumericColumns] = orginalData[NumericColumns].fillna(0).astype('Int64')

but it keeps resulting in the same error

TypeError: cannot safely cast non-equivalent float64 to int64

What can I do to convert the columns?

Asked By: Lostsoul

||

Answers:

Array must only contain whole numbers in order to safely convert float to int dtype. If you insist you can try as below:

orginalData[NumericColumns] = orginalData[NumericColumns].astype(int, errors='ignore')

As per your pandas version you can safely convert multiple columns at once. You don’t need to use apply for this.

Answered By: nimbous
import numpy as np
orginalData[NumericColumns] = orginalData[NumericColumns].fillna(0).astype(np.int64, errors='ignore')

For NaNs you need to replace the NaNs with 0, then do the type casting

Answered By: bigbounty

No need to replace nan.
You can pass to Int64 safely by doing:

df['A'] = np.floor(pd.to_numeric(df['A'], errors='coerce')).astype('Int64')

Your nans will be replaced with <NA>.
Source

You need to have pandas >.24 version.

Answered By: Hrvoje

The accepted solution is not correct in all cases, re: "np.floor". From the documentation at:
https://numpy.org/doc/stable/reference/generated/numpy.floor.html

"The floor of the scalar x is the largest integer i, such that i <= x. It is often denoted as [x]."

This will turn 9.99 to 9.00, and any downstream calculations will be inaccurate relative to the original data set.

Answered By: Dmot
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.