How NumPy create nan array in Python [6 Methods]

In this Python NumPy tutorial, I will explain how NumPy create nan array in Python using various methods with some illustrative examples.

To create a nan array in Python NumPy, we can directly assign the nan values, use the np.full function, the np.fill function, or modify the existing array with the nan values, the np.repeat() function, or can create a list of nan using the list comprehension, and convert it into an array.

NumPy create nan array in Python Methods

There are six different methods which can help us to create a nan array in Python:

  • Direct Initialization with nan
  • Using numpy.full
  • Modifying Existing Arrays
  • Using numpy.empty and fill
  • Using numpy.repeat
  • Using List Comprehension

Let’s see them one by one using some illustrative examples:

Method 1: Create nan array Python with np.nan

This method directly creates an array by specifying np.nan for each element in Python.

Here, is how it is done:

import numpy as np

nan_array = np.array([np.nan, np.nan, np.nan])
print(nan_array)

Output: The implementation of the code:

[nan nan nan]
numpy create nan array in Python

This way, numpy create nan array in Python with the np.nan.

Method 2: NumPy create array of nan using numpy.full

The np.full function fills an entire array of a specified shape with NaN in Python.

import numpy as np

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

Output: The output of the Python code is mentioned below:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]
create a nan array numpy in Python

This way we use the full function to NumPy create nan array in Python.

READ:  How to Use Django Built-In Login System

Method 3: array of nan Python by modifying the existing one

Here, we will convert all elements of an existing array to NaN, preserving its shape and type in Python.

import numpy as np

existing_array = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
existing_array[:] = np.nan

print(existing_array)

Output: The implementation of the code is mentioned below:

[nan nan nan nan nan]
create array of nans python

This way we can modify the array in NumPy to create nan array in Python.

Method 4: NumPy array of nan in Python using numpy.empty and fill

We create an uninitialized array using the np.empty() function and then fills it entirely with NaN using fill() function in Python.

import numpy as np

nan_array = np.empty((3, 3))
nan_array.fill(np.nan)
print(nan_array)

Output: The implementation of the code is as follows:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]
python nan array

This way, we can us the fill() with np.empty() function in NumPy create nan array in Python.

Method 5: NumPy nan array using numpy.repeat

This generates an array in Python by repeating NaN a specified number of times and reshaping it.

import numpy as np

nan_array = np.repeat(np.nan, 9).reshape(3, 3)
print(nan_array)

Output: The output is mentioned below:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]
numpy array of nans in Python

This way, NumPy create nan array in Python using np,repeat() function.

Method 6: Create array of nan in NumPy Python using list comprehension

List comprehension creates a list of NaN values in Python and then converts it into a NumPy array.

import numpy as np

nan_array = np.array([np.nan for _ in range(9)]).reshape(3, 3)
print(nan_array)

Output: The implementation of the code:

[[nan nan nan]
 [nan nan nan]
 [nan nan nan]]
create list of nans python

This way we can use list comprehension, where NumPy create nan array in Python.

READ:  PyTorch Tensor to Numpy

Conclusion

Understanding how NumPy create nan array in Python, using six different methods such as initialization with np.nan, using numpy.full, using numpy.empty and fill, using numpy.repeat, using List Comprehension, or by modifying the existing array with examples can help us to handle missing data in datasets.

Each method has its unique advantages and can be chosen based on the specific requirements of the task.

You may also like to read: