Create Array of All NaN Values in Python

Using numpy.empty() Function

To create an array of all NaN values in Python:

  • Use numpy.empty() to get an array of the given shape.
  • Assign numpy.nan to every array element using the assignment operator (=).

We used numpy.empty() to get an array of the given shape and dtype. Here, shape means the number of rows and columns (we used 3 by 3), and dtype means the data type of the returned/output array; by default, it is numpy.float64.

Next, we used the assignment operator (=) to assign nan to all the array entries. We could also use the ndarray.fill() method to fill all the entries with nan as follows:

Using numpy.full() Function

To create an array of all nan values in Python, use the numpy.full() function to get an array of the specified shape (rows and columns) and fill value.

To work with arrays, we must have numpy installed on our machine. We can install it using the pip command as pip install numpy on Windows operating system if it is not installed yet. You can check this article to install numpy on MacBook.

We used the numpy.full() function to get an array of the given shape (rows and columns) and fill value. This function took the following two parameters:

  1. shape – We used it to specify the number of rows and columns. Here, we created a matrix of 3 by 3.
  2. fill_value – It denotes the fill value, which was used to fill the array slots. We filled the array with nan.

The numpy.full() function can also take the dtype, order, and like parameters. Here, the dtype is an optional parameter and denotes the data type of the required array.

The order parameter is also optional and denotes F_contiguous or C_contiguous order; these are the orders in which we can store multidimensional data.

Like dtype and order, the like parameter is optional too; it represents the array_like object or prototype.

Using numpy.tile() Function

To create an array of all nan values in Python:

  • Use numpy.array() to create a 0-dimensional array with the given value.
  • Use the numpy.tile() function to get an array of the specified shape (rows and columns) and fill value.

After importing the numpy module, we used numpy.array() to create a 0-D (0 dimensional) array with value nan. Note that using this function, we can create an array of 1, 2, 3, 4, 5, or any dimensions.

Next, we used the numpy.tile() function to create an array of all nan values in Python. This function took two parameters, A (input array) and reps (number of repetitions of A along every axis).

The numpy.tile() function took an array as an input and returned an array repeated by the specified number of repetitions. How? For instance, if we pass a tuple (1,2), the numpy module will tile the original array once along the first and twice along the second dimensions.

Remember that we can pass an array or array-like data structure to the parameter A. On the other hand, the reps parameter is a bit challenging because it can modify the array’s dimensions and define the shape in which we want to tile our array.

Using numpy.repeat() Function

To create an array of all nan values in Python:

  • Use np.array() with * operator to create two dimensional array.
  • Use the numpy.repeat() function to repeat the individual elements of the array according to the given number of repetitions.

First, we used numpy.array() with the * operator to have a 2-D array. Next, we used the numpy module’s repeat() function to repeat the individual elements of the array. This function took the following three parameters:

  • array – Denotes the input array (array-like).
  • repeat – Represents the required number of repetitions of every array element along a given axis.
  • axis – Denotes the axis along which we will repeat values. By default, it returns a flat array as an output. For a 1-D array, we have only one axis, which is axis 0, but for 2-D arrays, we have two axes, axis 1 on the x-axis and axis 0 on the y-axis.

In our code example, we repeated the nan thrice on the x-axis because we had set the value of the axis to 1.

Using Multiplication of numpy.ones() with nan

To create an array of all nan values in Python:

  • Use the numpy.ones() function to get an array of the given shape (the number of rows and columns).
  • Use the * operator to replace the default value of the array produced by the numpy.ones() function (used in the previous step) with the numpy.nan value.

Here, we used the numpy.ones() function to create a multidimensional array by specifying the shape parameter. Then, we used this function to get an array of the given shape, 3 by 3. Note that the array returned by numpy.ones() was filled with 1 by default.

So, we used the * operator to multiply every element with numpy.nan to update from 1 to nan. Finally, we used the print() function to print the resultant array.

Remember, the numpy.ones() function is similar to the numpy.zeros(). We can also pass dtype, order, and like parameters, which are the same as we learned while using the numpy.full() function.

That’s all about how to create array of all NaN values in Python

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *