Home > OS >  Numpy: ValueError: cannot convert float NaN to integer (Python)
Numpy: ValueError: cannot convert float NaN to integer (Python)

Time:06-29

I want to insert NaN at specific locations in A. However, there is an error. I attach the expected output.

import numpy as np
from numpy import NaN

A = np.array([10, 20, 30, 40, 50, 60, 70])
C=[2,4]

A=np.insert(A,C,NaN,axis=0)
print("A =",[A])

The error is

<module>
    A=np.insert(A,C,NaN,axis=0)

  File "<__array_function__ internals>", line 5, in insert

  File "C:\Users\USER\anaconda3\lib\site-packages\numpy\lib\function_base.py", line 4678, in insert
    new[tuple(slobj)] = values

ValueError: cannot convert float NaN to integer

The expected output is

[array([10, 20,  NaN, 30, 40,  NaN, 50, 60, 70])]

CodePudding user response:

Designate a type for your array of float32 (or float16, float64, etc. as appropriate)

import numpy as np

A = np.array([10, 20, 30, 40, 50, 60, 70], dtype=np.float32)
C=[2,4]

A=np.insert(A,C,np.NaN,axis=0)
print("A =",[A])

A = [array([10., 20., nan, 30., 40., nan, 50., 60., 70.], dtype=float32)]

CodePudding user response:

The way to fix this error is to deal with the NaN values before attempting to convert the column from a float to an integer.

you can follow the steps as follows

We can use the following code to first identify the rows that contain NaN values:

#print rows in DataFrame that contain NaN in 'rebounds' column
print(df[df['rebounds'].isnull()])

   points  assists  rebounds
1      12        7       NaN
5      23        9       NaN

We can then either drop the rows with NaN values or replace the NaN values with some other value before converting the column from a float to an integer:

Method 1: Drop Rows with NaN Values

#drop all rows with NaN values
df = df.dropna()

#convert 'rebounds' column from float to integer
df['rebounds'] = df['rebounds'].astype(int) 

#view updated DataFrame
df
    points  assists rebounds
0   25  5   11
2   15  7   10
3   14  9   6
4   19  12  5
6   25  9   9
7   29  4   12

#view class of 'rebounds' column
df['rebounds'].dtype

dtype('int64')

Method 2: Replace NaN Values

#replace all NaN values with zeros
df['rebounds'] = df['rebounds'].fillna(0)

#convert 'rebounds' column from float to integer
df['rebounds'] = df['rebounds'].astype(int) 

#view updated DataFrame
df

    points  assists rebounds
0   25  5   11
1   12  7   0
2   15  7   10
3   14  9   6
4   19  12  5
5   23  9   0
6   25  9   9
7   29  4   12

#view class of 'rebounds' column
df['rebounds'].dtype

dtype('int64')

good luck

  • Related