xarray.where#
- xarray.where(cond, x, y, keep_attrs=None)[source]#
Return elements from x or y depending on cond.
Performs xarray-like broadcasting across input arguments.
All dimension coordinates on x and y must be aligned with each other and with cond.
- Parameters:
cond (scalar, array,
Variable,DataArrayorDataset) – When True, return values from x, otherwise returns values from y.x (scalar, array,
Variable,DataArrayorDataset) – values to choose from where cond is Truey (scalar, array,
Variable,DataArrayorDataset) – values to choose from where cond is Falsekeep_attrs (
boolor{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}orcallable(), optional) –‘override’ or True (default): skip comparing and copy attrs from x to the result.
‘drop’ or False: empty attrs on returned xarray object.
‘identical’: all attrs must be the same on every object.
‘no_conflicts’: attrs from all objects are combined, any that have the same name must also have the same value.
‘drop_conflicts’: attrs from all objects are combined, any that have the same name but different values are dropped.
- Returns:
Dataset,DataArray,Variableor array – In priority order: Dataset, DataArray, Variable or array, whichever type appears as an input argument.
Examples
>>> x = xr.DataArray( ... 0.1 * np.arange(10), ... dims=["lat"], ... coords={"lat": np.arange(10)}, ... name="sst", ... attrs={"standard_name": "sea_surface_temperature"}, ... ) >>> x <xarray.DataArray 'sst' (lat: 10)> Size: 80B array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) Coordinates: * lat (lat) int64 80B 0 1 2 3 4 5 6 7 8 9 Attributes: standard_name: sea_surface_temperature
>>> xr.where(x < 0.5, x, x * 100) <xarray.DataArray 'sst' (lat: 10)> Size: 80B array([ 0. , 0.1, 0.2, 0.3, 0.4, 50. , 60. , 70. , 80. , 90. ]) Coordinates: * lat (lat) int64 80B 0 1 2 3 4 5 6 7 8 9 Attributes: standard_name: sea_surface_temperature
If x is a scalar then by default there are no attrs on the result
>>> xr.where(x < 0.5, 1, 0) <xarray.DataArray 'sst' (lat: 10)> Size: 80B array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) Coordinates: * lat (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
If x is a scalar (and therefore has no attrs), preserve the attrs on cond by using keep_attrs=”drop_conflicts”
>>> xr.where(x < 0.5, 1, 0, keep_attrs="drop_conflicts") <xarray.DataArray 'sst' (lat: 10)> Size: 80B array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) Coordinates: * lat (lat) int64 80B 0 1 2 3 4 5 6 7 8 9 Attributes: standard_name: sea_surface_temperature
>>> y = xr.DataArray( ... 0.1 * np.arange(9).reshape(3, 3), ... dims=["lat", "lon"], ... coords={"lat": np.arange(3), "lon": 10 + np.arange(3)}, ... name="sst", ... ) >>> y <xarray.DataArray 'sst' (lat: 3, lon: 3)> Size: 72B array([[0. , 0.1, 0.2], [0.3, 0.4, 0.5], [0.6, 0.7, 0.8]]) Coordinates: * lat (lat) int64 24B 0 1 2 * lon (lon) int64 24B 10 11 12
>>> xr.where(y.lat < 1, y, -1) <xarray.DataArray 'lat' (lat: 3, lon: 3)> Size: 72B array([[ 0. , 0.1, 0.2], [-1. , -1. , -1. ], [-1. , -1. , -1. ]]) Coordinates: * lat (lat) int64 24B 0 1 2 * lon (lon) int64 24B 10 11 12
>>> cond = xr.DataArray([True, False], dims=["x"]) >>> x = xr.DataArray([1, 2], dims=["y"]) >>> xr.where(cond, x, 0) <xarray.DataArray (x: 2, y: 2)> Size: 32B array([[1, 2], [0, 0]]) Dimensions without coordinates: x, y