where#
- where(cond, if_true, if_false, master_xda: DataArray | None = None, new_name: str = '') DataArray[source]#
Overloads
xr.wherewith:setting metadata of
master_xdapreserving the nodata pixels of the
master_xda
If
master_xdais None, use it likexr.where. Else, it outputs axarray.DataArraywith the same dtype thanmaster_xda.Warning
If you don’t give a
master_xda, it is better to pass numpy arrays toif_falseandif_truekeywords as passing xarrays interfers with the output metadata (you may lose the CRS and so on). Just passif_true=true_xda.datainplace ofif_true=true_xdaand the same forif_false- Parameters:
cond (scalar, array, Variable, DataArray or Dataset) – Conditional array
if_true (scalar, array, Variable, DataArray or Dataset) – What to do if
condis Trueif_false (scalar, array, Variable, DataArray or Dataset) – What to do if
condis Falsemaster_xda – Master
xr.DataArrayused to set the metadata and the nodatanew_name (str) – New name of the array
- Returns:
Where array with correct mtd and nodata pixels
- Return type:
xr.DataArray
Example
>>> A = xr.DataArray(dims=("x", "y"), data=[[1, 0, 5], [np.nan, 0, 0]]) >>> mask_A = rasters.where(A > 3, 0, 1, A, new_name="mask_A") <xarray.DataArray 'mask_A' (x: 2, y: 3)> array([[ 1., 1., 0.], [nan, 1., 1.]]) Dimensions without coordinates: x, y