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

s 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 `RuntimeWarning`

s.

## 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!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

**You build high-value coding skills by working on practical coding projects!**

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is ** YES!**, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.