You can create a masked array like this:

data = np.array([[1,2,3], [4,5,np.NaN], [np.NaN,6,np.NaN], [0,0,0]])
masked_data = np.ma.masked_array(data, np.isnan(data))
# calculate your weighted average here instead
weights = [1, 1, 1]
average = np.ma.average(masked_data, axis=1, weights=weights)
# this gives you the result
result = average.filled(np.nan)
print(result)

This outputs:

[ 2.   4.5  6.   0. ]
Answer from Alex 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.
🌐
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
🌐
CSDN
devpress.csdn.net › python › 630463e97e6682346619af02.html
Taking np.average while ignoring NaN's? - DevPress官方社区
August 23, 2022 - data = np.array([[1,2,3], [4,5,np.NaN], [np.NaN,6,np.NaN], [0,0,0]]) masked_data = np.ma.masked_array(data, np.isnan(data)) # calculate your weighted average here instead weights = [1, 1, 1] average = np.ma.average(masked_data, axis=1, weights=weights) # this gives you the result result = average.filled(np.nan) print(result) ... 问题:如何重塑熊猫。系列 在我看来,它就像 pandas.Series 中的一个错误。 a = pd.Series([1,2,3,4]) b = a.reshape(2,2) b b 有类型 Series 但无法显示,最后一条语句给出异常,非常冗长,最后一行是“TypeError: %d format: a number is required, not numpy.ndarray”。 b.sha
🌐
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 - 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 larger than zero. Drilling this down, I reached np.multiply(), but I'm not familiar enough with python/C to dig deeper.. import sys import numpy as np print(np.average([1, 1], weights=[1, 0])) # 1.0 print(np.average([np.inf, 1], weights=[1, 1])) # inf print(np.average([np.inf, 1], weights=[1, 0])) # inf print(np.average([np.inf, 1], weights=[0, 1])) # nan # RuntimeWarning: invalid value encountered in multiply avg = np.multiply(a, wgt, eps = sys.float_info.epsilon print(np.average([np.inf, 1], weights=[0+eps, 1])) # inf ·
Author: numpy
🌐
AlgoCademy
algocademy.com › blog › understanding-np-mean-a-comprehensive-guide-to-calculating-averages-in-numpy
Understanding np.mean: A Comprehensive Guide to Calculating Averages in NumPy – AlgoCademy Blog
To handle NaN values, NumPy provides np.nanmean(), which ignores NaN values when computing the mean: # Using nanmean to ignore NaN values nan_mean = np.nanmean(arr_with_nan) print(f"Mean ignoring NaNs: {nan_mean}") # Output: 3.0 · Sometimes, not all values in your data should contribute equally to the mean. In such cases, you can use np.average() to calculate a weighted mean:
Find elsewhere
🌐
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.
🌐
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.
🌐
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 - Including those missing scores as zeros would skew the results. Ignoring them, on the other hand, gives you a fair average. That’s exactly the kind of problem we face with nan values in NumPy arrays.
🌐
Codecademy
codecademy.com › article › hands-on-statistics-with-numpy-in-python
Hands-on Statistics with NumPy in Python | Codecademy
Similarly, we get a nan value at the third position in the output array while calculating the weighted mean for elements in each column. NumPy doesn’t have a function like nanaverage() to ignore nan values and calculate the weighted mean for the rest of the values in the input arrays.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.ewm.html
pandas.DataFrame.ewm — pandas 3.0.6 documentation
When ignore_na=True, weights are based on relative positions. For example, the weights of \(x_0\) and \(x_2\) used in calculating the final weighted average of [\(x_0\), None, \(x_2\)] are \(1-\alpha\) and \(1\) if adjust=True, and \(1-\alpha\) and \(\alpha\) if adjust=False.
🌐
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.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.average.html
numpy.average — NumPy v2.5 Manual
Each value in a contributes to ... must have dimensions and shape consistent with a along the specified axis. If weights=None, then all data in a are assumed to have a weight equal to one....
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.average.html
numpy.average — NumPy v1.25 Manual
If axis is a tuple of ints, averaging is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before. ... An array of weights associated with the values in a. Each value in a contributes to the average according to its associated weight.
🌐
SciPy
docs.scipy.org › doc › numpy-1.14.0 › reference › generated › numpy.nanmean.html
numpy.nanmean — NumPy v1.14 Manual
numpy.nanmean(a, axis=None, dtype=None, out=None, keepdims=<class 'numpy._globals._NoValue'>)[source]¶ · 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 ...
🌐
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.