# 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]``

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

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

### 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]``

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

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

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

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