Alternatively, you can use a MaskedArray as such:

>>> import numpy as np

>>> a = np.array([1,2,np.nan,4])
>>> weights = np.array([4,3,2,1])
>>> ma = np.ma.MaskedArray(a, mask=np.isnan(a))
>>> np.ma.average(ma, weights=weights)
1.75
Answer from Nicolas Barbey on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.5 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.2 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.6.dev0 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.1 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
GeeksforGeeks
geeksforgeeks.org › python-numpy-nanmean-function
Python | numpy.nanmean() function - GeeksforGeeks
June 1, 2021 - The input array will be modified by the call to median. keepdims: If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a. Returns: Returns the average of the array elements ... # Python code to demonstrate the # use of numpy.nanmean import numpy as np # create 2d array with nan value.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v2.0 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › nanmean()
Python Numpy nanmean() - Calculate Mean Ignoring NaN
November 19, 2024 - Use the numpy.nanmean() function to compute the mean while skipping NaN values.
Find elsewhere
🌐
Medium
medium.com › @heyamit10 › how-to-use-numpy-nanmean-to-calculate-the-mean-while-ignoring-nan-f5da3320be87
How to use numpy.nanmean() to Calculate the Mean While Ignoring nan | by Hey Amit | Medium
February 8, 2025 - This shows how quickly NumPy handles even a dataset with a million entries while ignoring nan values. Q: What happens if all values in an array or along an axis are nan? This might surprise you: if all the values are nan, the result will also be nan. It’s like asking for the average of nothing—Python simply can’t compute it.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy nanmean() – get mean ignoring nan values
NumPy nanmean() - Get Mean ignoring NAN Values - Spark By {Examples}
March 27, 2024 - Python NumPy nanmean() function is used to compute the arithmetic mean or average of the array along a specified axis while ignoring NaN (Not a Number)
🌐
IncludeHelp
includehelp.com › python › numpys-mean-and-nanmean-methods.aspx
Python - NumPy's mean() and nanmean() Methods
October 9, 2023 - The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs. While on the other hand, the numpy.nanmean() computes the arithmetic mean along the specified axis, ignoring NaNs.
🌐
GitHub
github.com › numpy › numpy › issues › 25362
BUG: average() returns nan when an `inf` value is given a 0 weight · Issue #25362 · numpy/numpy
December 10, 2023 - Describe the issue: When I try to compute the weighted mean of an array that includes inf, and that element is given a weight of 0, I'm getting a RuntimeWarning, and a nan result. This isn't the case when the weight is just slightly larg...
Author: numpy
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-replace-nan-values-with-average-of-columns
Replace NaN Values with Average of Columns - NumPy - GeeksforGeeks
September 27, 2025 - This method creates a masked array where NaNs are ignored and uses np.where to build a new array with column averages replacing NaNs.
🌐
CodeRivers
coderivers.org › blog › python-average-with-nan-values
Python Average with Nan Values: A Comprehensive Guide - CodeRivers
February 3, 2025 - Before calculating the average, it's important to identify where nan values are present in the data. In numpy, we can use np.isnan() to check for nan values in an array.
🌐
Quora
quora.com › How-do-I-get-the-average-value-in-Python-of-a-list-containing-NAN-value
How to get the average value in Python of a list containing NAN value - Quora
Answer (1 of 4): Depending on what you mean by ignore, you can use numpy’s nanmean function: [code]import numpy as np ls = np.ones(10) ls[3] = np.nan np.nanmean(ls) # prints 1.0 [/code]This will calculate the mean filtering out the NaNs. Forcing the NaNs to a specific value in the calculation...
🌐
University of Texas at Austin
het.as.utexas.edu › HET › Software › Numpy › reference › generated › numpy.nanstd.html
numpy.nanstd — NumPy v1.9 Manual
For all-NaN slices or slices with zero degrees of freedom, NaN is returned and a RuntimeWarning is raised. New in version 1.8.0. ... The standard deviation is the square root of the average of the squared deviations from the mean: std = sqrt(mean(abs(x - x.mean())**2)).
🌐
GitHub
github.com › numpy › numpy › issues › 21375
ENH: np.nanmean with weights · Issue #21375 · numpy/numpy
April 21, 2022 - Wrapper for np.average, with np.nan values being ignored from the average This is similar to np.nanmean, but allowing to pass weights as in np.average · The ideas would be to add this functionality to one of the two functions, i.e: a) add weights to np.nanmean: https://numpy.org/doc/stable/reference/generated/numpy.nanmean.html b) add option to ignore nan's for np.average : https://numpy.org/doc/stable/reference/generated/numpy.average.html ·
Author: numpy
🌐
University of Texas at Austin
het.as.utexas.edu › HET › Software › Numpy › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v1.9 Manual
Compute the arithmetic mean along the specified axis, ignoring NaNs. Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.