4

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])]
2
  • 1
    There is no NAN in the integer type. It only exists in the floating type. You cannot convert it to an integer. Commented Jun 29, 2022 at 6:28
  • 1
    Right. Your choices are (a) convert the array to float, or (b) use some other flag value. Commented Jun 29, 2022 at 6:29

2 Answers 2

4

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)]

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Please cite your source if you have used information from elsewhere verbatim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.