How to Create a NumPy Array and Fill It With NaN Values? 

Rate this post

NaN stands for Not A Number. It is a placeholder to indicate the absence of value. One can think of it as a None or 0. It’s just that None or 0 belongs to a specific type. However, NaN doesn’t.

We use NaN to represent missing data or the failure to perform some operation. While performing complex computations, if the user tries to do an impossible task (say, performing a logarithm of a negative value), instead of throwing an error and crashing the program, NaN is returned.

NumPy defines NaN as a constant value. To use it in your program, use numpy.nan

import numpy as np
np.nan

In this article, let’s learn how to create an array filled with NaN values.

Method 1: Using numpy.full() 

One of the simplest ways to create a NumPy array with NaN values is by using the numpy.full() method. However, numpy.full() is available in NumPy versions 1.8+.

To create a numpy array with rows number of rows and cols number of columns filled in NaN values, use the following syntax:

np.full((rows,cols),np.nan)

Example:

In the below code snippet, let’s create a 3*3 array filled with Nan values.

import numpy as np

arr=np.full((3,3),np.nan)
print(arr)

Output:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]

Method 2: Using numpy.fill() 

You can create an empty array and then fill the array values to NaN using the numpy.fill().

Although there are many ways of creating an empty array, let’s use the simplest one, numpy.empty().

To create an empty NumPy array with rows number of rows and cols number of columns, and fill in NaN values, use the following.

np.empty((rows,columns))    # create an empty array of (n,m) shape
np.fill(np.nan)             # fill the array with Nan values

Let’s say you want to create a Nan array of size 3*3 and fill it with NaN arrays.

import numpy as np

arr=np.empty((3,3))     #Create an empty array
arr.fill(np.NaN)        #Fill the array with Nan values
print(arr)

Output:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]

Method 3: Using arr[:] to Fill NaN Values

After creating an empty array, instead of numpy.fill(), you can also use the array slicing to assign NaN values.

To specify the NaN value to the entire array, use the below statement.

arr[:] = np.nan 

Firstly, create an empty array of the required shape and then assign it with NaN values as shown.

import numpy as np

arr=np.empty((3,3))
arr[:]=np.nan
print(arr)

Output:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]

Method 4: Using numpy.tile() 

Numpy allows us to construct an array by repeating an element n number of times using numpy.tile().

To construct a NaN array with rows number of rows and cols number of columns, 

np.tile(np.nan, (rows, cols))

For example, to construct an array of size 3*3 and fill it with NaN values, use the following:

import numpy as np

arr=np.tile(np.nan,(3,3))
print(arr)

Output:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]

Method 5: Using numpy.repeat() 

You can create a NaN array and repeat it the required number of times.

To create a NaN array with rows number rows and cols number of columns, use the numpy.repeat() method as shown below.

np.repeat([[np.nan]]*rows, cols, axis=1)

Let’s say you want to create an array with 2 rows and 3 columns, you can run the below code snippet.

import numpy as np

arr=np.repeat([[np.nan]]*2,3,axis=1)
print(arr)

Output:

[[nan nan nan]
 [nan nan nan]]

Method 6: Using numpy.ones() * Nan

Trying to remember the names of the NumPy methods can be overwhelming. If that is the case, you can create a NumPy array with ones and multiply it with NaN values.

Firstly, create an array of the shape (rows,cols) loaded with ones, using the numpy.ones() method. Then multiply the array with the NaN value as shown below.

np.ones((rows,cols))* np.nan

If you want to create a 3*3 Nan array, try the following:

import numpy as np

arr=np.ones((3,3))* np.nan
print(arr)

Output:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]

Method 7: Multiplying the List

Create a list with NaNn values and convert it to a NumPy array using the np.array() method.

To create a Nan array with rows no. of rows and cols no. of columns, use the following,

arr = np.array(cols * [rows*[np.nan]])

For example, if you want to create a NaN array with 2 rows and 3 columns, use the below code snippet:

import numpy as np

arr = np.array(3 * [2*[np.nan]])
print(arr)

Output:

[[nan nan]
 [nan nan]
 [nan nan]]

Execution Times and Runtime Comparison

Now that we’ve seen the different methods available to create an empty NaN array, let’s determine the execution time in nanoseconds for all of the above methods.

To create a NaN array of size 3*3 let’s check how much time each method is going to take. Let’s use the perf_counter_ns from the time module to determine the execution time.

import numpy as np
from time import perf_counter_ns

#Using np.fill()
start=perf_counter_ns()
arr=np.empty((3,3))     #create an empty array
arr.fill(np.NaN)        #Fill the array with Nan values
end=perf_counter_ns()
print("Execution Time using fill   : %5dns"%(end-start))

#Using the slicing(colon)
start=perf_counter_ns()
arr_colon=np.empty((3,3))
arr_colon[:]=np.nan
end=perf_counter_ns()
print("Execution Time using colon  : %5dns"%(end-start))

#Using the np.full()
start=perf_counter_ns()
arr_full=np.full((3,3),np.nan)
end=perf_counter_ns()
print("Execution Time using full   : %5dns"%(end-start))

#Using np.tile()
start=perf_counter_ns()
arr_tile=np.tile(np.nan,(3,3))
end=perf_counter_ns()
print("Execution Time using tile   : %5dns"%(end-start))

#Using np.repeat()
start=perf_counter_ns()
arr_repeat=np.repeat([[np.nan]]*3,3,axis=1)
end=perf_counter_ns()
print("Execution Time using repeat : %5dns"%(end-start))


#Using np.ones*np.nan
start=perf_counter_ns()
arr_ones=np.ones((3,3))* np.nan
end=perf_counter_ns()
print("Execution Time using ones   : %5dns"%(end-start))

#Using list
start=perf_counter_ns()
arr_list = np.array(3 * [3*[np.nan]])
end=perf_counter_ns()
print("Execution Time using list   : %5dns"%(end-start))

Output:

Execution Time using fill   :  4100ns
Execution Time using colon  :  2700ns
Execution Time using full   :  4600ns
Execution Time using tile   : 18500ns
Execution Time using repeat :  9700ns
Execution Time using ones   :  8000ns
Execution Time using list   :  2300ns

Conclusion

This brings us to the end of this article.

In this article, we have discovered various ways of creating a numpy array with NaN values.

We’ve also seen the execution times for different methods. Thanks for reading. Please subscribe to our channel and our email academy to keep learning.