Skip to Content

Numpy – Set All Zeros to NaN

The Numpy library in Python comes with a number of useful functions and methods to work with and manipulate the data in arrays. In this tutorial, we will look at how to set all the zeros in a Numpy array to nan with the help of some examples.

Steps to set all the zeros to nan in Numpy

Set all zeros to nan in numpy array

You can use boolean indexing to set all the zeros in a Numpy array to nan. The following is the syntax –

# set all zeros to nan
ar[ar == 0] = np.nan

It replaces the occurrences of the value 0 in the array ar with nan.

Let’s now look at a step-by-step example of using this syntax –

Step 1 – Create a Numpy array

First, we will create a Numpy array that we will use throughout this tutorial.

import numpy as np

# create numpy array
ar = np.array([-3, -2, -1, 0, 1, 2, 3, 0, 4, 5])
# display the array
print(ar)

Output:

[-3 -2 -1  0  1  2  3  0  4  5]

Here, we used the numpy.array() function to create a one-dimensional Numpy array containing some numbers. You can see that the array has both positive and negative values (along with some zeros).

Step 2 – Convert the array to float type

If the array contains only integer values, first convert them to float. This is because np.nan is of float type and if you try to fill a float value in an integer array, you’ll get an error.

For example –

# set zeros to nan
ar[ar == 0] = np.nan

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [2], in <module>
      1 # set zeros to nan
----> 2 ar[ar == 0] = np.nan

ValueError: cannot convert float NaN to integer

Here we get an error because we’re setting the value inside an integer array with a float value.

Thus, first, convert the array to float type.

# convert to float
ar = ar.astype('float')
# display the array
print(ar)

Output:

[-3. -2. -1.  0.  1.  2.  3.  0.  4.  5.]

Step 3 – Replace zeros with nan using boolean indexing

Using boolean indexing identify the values that are equal to zero and then set them to nan.

First, we will specify our boolean expression ar == 0 (which finds the zero values in the array) and then set values satisfying this condition to nan.

# set zeros to nan
ar[ar == 0] = np.nan
# display the array
print(ar)

Output:

[-3. -2. -1. nan  1.  2.  3. nan  4.  5.]

The resulting array has all the zeros replaced with nan.

To understand what’s happening here, let’s look under the hood. Let’s see what we get from the expression ar == 0

# create a numpy array
ar = np.array([-3, -2, -1, 0, 1, 2, 3, 0, 4, 5])
# result of boolean expression ar == 0
ar == 0

Output:

array([False, False, False,  True, False, False, False,  True, False,
       False])

We get a boolean array. The boolean values in this array represent whether a value at a particular index satisfies the given condition (in our case whether the element is equal to zero or not).

When we do ar[ar == 0] = np.nan, we are essentially setting the values in the array where the condition evaluates to True to np.nan.

You can similarly filter a Numpy array for other conditions as well.

Summary – Set zeros to nan in Numpy

In this tutorial, we looked at how to replace all the zeros in a Numpy array with nan. The following is a short summary of the steps mentioned –

  1. Create a Numpy array (skip this step if you already have an array to operate on).
  2. Convert the array to float type if all the values inside the array are integers.
  3. Use boolean indexing to find the zeros and then set them to nan –
    ar[ar == 0] = np.nan

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.