Reut
Reut

Reputation: 1592

numpy change specific value to null gives error "ValueError: cannot convert float NaN to integer"

I have numpy array with the shape of (1,5,10).

array([[[ -5,  -5,  -5,  -5, 120, 116, 118, 118,  -5,  -5],
        [ -5,  -5, 126, 127, 125, 118, 118, 123,  -5,  -5],
        [ -5, 121, 125, 118, 115, 115, 121, 121, 114, 127],
        [112, 118, 108, 111, 110, 112, 104, 102, 103,  -5],
        [105, 108, 107,  -5,  -5,  -5,  -5,  -5,  -5,  -5]]], dtype=int16)

I would like to change all the -5 into np.nan value. In order to do that I have written this code:

out_image[out_image == (-5)] = np.nan

but that gives me an error:

ValueError: cannot convert float NaN to integer

Why do I get this error? and how can I replace the values into nan?

Upvotes: 1

Views: 757

Answers (3)

Saiyam Jain
Saiyam Jain

Reputation: 161

I was also facing same issue ,when I was storing my Data into BigInt column and to counter the issue to store Nan/Null/None into int/BigInt datatype I have used below approach.

df['column_name'] = df['column_name'].convert_dtypes()
df['column_name'] = df['column_name'].astype('Int64')

using first line code I have converted all values into float and then with below code I have converted back everything into Int64

you can directly jump to second part for me i have to do some other changes into my column values that's why I have used convert_dtypes()

Upvotes: 0

SM1312
SM1312

Reputation: 588

You just need to convert it to float first.

out_image = out_image.astype('float')
out_image[out_image== -5] = np.NAN

Upvotes: 3

lemon
lemon

Reputation: 15482

You're doing it as out_image were a pandas.Dataframe. Instead you can use numpy.where method like this:

np.where(out_image == -5, np.NaN, out_image)

Upvotes: 3

Related Questions