[Solved] NumPy RuntimeWarning: All-NaN slice encountered

Problem Formulation

You use NumPy’s np.nanmedian() function in your code that is supposed to ignore NaN values when computing the mean of a NumPy array.

import numpy as np
a = np.array([np.NaN, np.NaN])
mean = np.nanmedian(a)

But when using it, NumPy raises a RuntimeWarning: All-NaN slice encountered message:

Warning (from warnings module):
  File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\nanfunctions.py", line 1114
    overwrite_input=overwrite_input)
RuntimeWarning: All-NaN slice encountered

What is the reason for this warning and how to fix it?

Solution + Explanation

The reason this warning arises is because you apply the np.nanmedian() function on an empty array. The function doesn’t cause an error if the array has at least one non-NaN value:

>>> np.nanmedian([0.42, np.NaN, np.NaN])
0.42

If at least one element is a numerical value, the mean is clearly defined: get the median of all not NaN elements.

However, if all elements in a NumPy array are np.NaN, NumPy raises the RuntimeWarning:

import numpy as np
a = np.array([np.NaN, np.NaN])
mean = np.nanmedian(a)
print(mean)
'''
OUTPUT:
Warning (from warnings module):
  File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\nanfunctions.py", line 1114
    overwrite_input=overwrite_input)
RuntimeWarning: All-NaN slice encountered
nan
'''

Yet, you can also see that it still generates the return value: not-a-number or nan.

As this border case is properly defined and unambiguous, this has caused some programmers to ask whether it makes even sense to issue this warning.

💡 In my opinion, issuing a warning doesn’t make a lot of sense in the case of the np.nanmedian() function. From Python’s Zen of Python, we know that “explicit is better than implicit”. So, either raise an exception and let the programmer handle it directly or just let it go through if everything is properly defined.

If, like me, you’re annoyed by this warning, you can simply suppress it:

How to Suppress RuntimeWarning?

The context manager warnings.catch_warnings suppresses the warning, but only if you indeed anticipate it coming. Otherwise, you may miss some additional RuntimeWarnings you didn’t see coming.

import numpy as np
import warnings


with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=RuntimeWarning)
    median = np.nanmedian([np.NaN, np.NaN])
    print(median)

The output is without warning:

nan

However, you need to be sure that this is the only warning that can appear in the with block environment. Otherwise, you may miss some important warnings as already discussed.

A safer way would probably be to use warnings.filterwarnings(action='ignore', message='All-NaN slice encountered') to let through not anticipated RuntimeWarnings.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!