MCPcopy Index your code
hub / github.com/pydata/xarray / _sum_of_weights

Method _sum_of_weights

xarray/computation/weighted.py:234–255  ·  view source on GitHub ↗

Calculate the sum of weights, accounting for missing values

(self, da: T_DataArray, dim: Dims = None)

Source from the content-addressed store, hash-verified

232 return dot(da, weights, dim=dim)
233
234 def _sum_of_weights(self, da: T_DataArray, dim: Dims = None) -> T_DataArray:
235 """Calculate the sum of weights, accounting for missing values"""
236
237 # we need to mask data values that are nan; else the weights are wrong
238 mask = da.notnull()
239
240 # bool -> int, because ``xr.dot([True, True], [True, True])`` -> True
241 # (and not 2); GH4074
242 if self.weights.dtype == bool:
243 sum_of_weights = self._reduce(
244 mask,
245 duck_array_ops.astype(self.weights, dtype=int),
246 dim=dim,
247 skipna=False,
248 )
249 else:
250 sum_of_weights = self._reduce(mask, self.weights, dim=dim, skipna=False)
251
252 # 0-weights are not valid
253 valid_weights = sum_of_weights != 0.0
254
255 return sum_of_weights.where(valid_weights)
256
257 def _sum_of_squares(
258 self,

Callers 2

_weighted_meanMethod · 0.95
_weighted_varMethod · 0.95

Calls 4

_reduceMethod · 0.95
notnullMethod · 0.45
astypeMethod · 0.45
whereMethod · 0.45

Tested by

no test coverage detected