[Solved] NumPy RuntimeWarning: Mean of empty slice

Problem Formulation

You use NumPy’s np.nanmean() 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.nanmean(a)

But when using it, NumPy raises a RuntimeWarning: Mean of empty slice message:

Warning (from warnings module):
  File "C:\Users\xcent\Desktop\code.py", line 3
    mean = np.nanmean(a)
RuntimeWarning: Mean of empty slice

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.nanmean() function on an empty array. The function doesn’t cause an error if the array has at least one non-NaN value:

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

If at least one element is a numerical value, the mean is clearly defined: sum over all elements that are not NaN and divide by the number of those elements.

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

>>> np.nanmean([np.NaN, np.NaN])

Warning (from warnings module):
  File "C:\Users\xcent\Desktop\code.py", line 1
    import numpy as np
RuntimeWarning: Mean of empty slice
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.nanmean() 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)
    mean = np.nanmean([np.NaN, np.NaN])
    print(mean)

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='Mean of empty slice') to let through not anticipated RuntimeWarnings.

Alternative Solution: Check for all-NaN Values

If you don’t like the previous solution for its lack of brevity—like me—you can also defensively check if the array contains only NaN values. If it does, you simply hard-code the solution to be nan without even running the np.nanmean() function that generates the warning message.

The following code creates a custom function numpy_nan_mean() that takes an array as input and returns the mean or nan if all values are np.NaN.

import numpy as np


def numpy_nan_mean(a):
    return np.NaN if np.all(a!=a) else np.nanmean(a)

print(numpy_nan_mean([np.NaN, np.NaN, np.NaN]))
# nan

print(numpy_nan_mean([np.NaN, np.NaN, 1.23]))
# 1.23

The code uses the observation that comparing two np.NaN values will always return False. Only if all values are np.Nan will the function call np.all(a!=a) return True.

>>> np.NaN == np.NaN
False

We use the ternary one-liner operator x if y else z to return np.NaN in that particular case instead of executing the np.nanmean() function that would produce the warning.

RuntimeWarning – Calculating Mean From Empty Array

Interestingly, there’s another source of this warning message: if you try to calculate the np.nanmean([]) of an empty NumPy array or empty list:

>>> np.nanmean([])

Warning (from warnings module):
  File "C:\Users\xcent\Desktop\code.py", line 1
    import numpy as np
RuntimeWarning: Mean of empty slice
nan

You can fix this by first checking the array for emptiness and only calculating the mean if it is non-empty:

>>> def nanmean(a):
	if a.size == 0:
		return np.NaN
	else:
		return np.nanmean(a)

	
>>> nanmean(np.array([]))
nan
>>> nanmean(np.array([1, 2, 3]))
2.0

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!