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
However, if all elements in a NumPy array are
np.NaN, NumPy raises the
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
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:
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
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.