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?


import_contacts

You may also like:

Related Posts

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusion Fill array with random numbers […]

  • 17 May

    Add Month to datetime in Python

    Table of ContentsWays to add month to datetime in PythonUsing the datetime.datetime constructor to add month to datetime in Python.Using the dateutil.relativedelta object to add month to datetime in PythonUsing the pandas.DateOffset() function to add month to datetime in PythonUsing the arrow.shift() function to add month to datetime in PythonConclusion In Python, we can represent […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusion In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how we can append […]

  • Python array size
    17 December

    Python array size: get size of array in Python

    Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusion In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. In Python, we […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.