Create a Weighted object Parameters ---------- obj : DataArray or Dataset Object over which the weighted reduction operation is applied. weights : DataArray An array of weights associated with the values in the obj. Each v
(self, obj: T_Xarray, weights: T_DataArray)
| 147 | __slots__ = ("obj", "weights") |
| 148 | |
| 149 | def __init__(self, obj: T_Xarray, weights: T_DataArray) -> None: |
| 150 | """ |
| 151 | Create a Weighted object |
| 152 | |
| 153 | Parameters |
| 154 | ---------- |
| 155 | obj : DataArray or Dataset |
| 156 | Object over which the weighted reduction operation is applied. |
| 157 | weights : DataArray |
| 158 | An array of weights associated with the values in the obj. |
| 159 | Each value in the obj contributes to the reduction operation |
| 160 | according to its associated weight. |
| 161 | |
| 162 | Notes |
| 163 | ----- |
| 164 | ``weights`` must be a ``DataArray`` and cannot contain missing values. |
| 165 | Missing values can be replaced by ``weights.fillna(0)``. |
| 166 | """ |
| 167 | |
| 168 | from xarray.core.dataarray import DataArray |
| 169 | |
| 170 | if not isinstance(weights, DataArray): |
| 171 | raise ValueError("`weights` must be a DataArray") |
| 172 | |
| 173 | def _weight_check(w): |
| 174 | # Ref https://github.com/pydata/xarray/pull/4559/files#r515968670 |
| 175 | if duck_array_ops.array_any(duck_array_ops.isnull(w)): |
| 176 | raise ValueError( |
| 177 | "`weights` cannot contain missing values. " |
| 178 | "Missing values can be replaced by `weights.fillna(0)`." |
| 179 | ) |
| 180 | return w |
| 181 | |
| 182 | if is_duck_dask_array(weights.data): |
| 183 | # assign to copy - else the check is not triggered |
| 184 | weights = weights.copy( |
| 185 | data=weights.data.map_blocks(_weight_check, dtype=weights.dtype), # type: ignore[call-arg, arg-type] |
| 186 | deep=False, |
| 187 | ) |
| 188 | |
| 189 | else: |
| 190 | _weight_check(weights.data) |
| 191 | |
| 192 | self.obj: T_Xarray = obj |
| 193 | self.weights: T_DataArray = weights |
| 194 | |
| 195 | def _check_dim(self, dim: Dims): |
| 196 | """raise an error if any dimension is missing""" |
nothing calls this directly
no test coverage detected