MCPcopy Create free account
hub / github.com/pydata/xarray / where

Function where

xarray/computation/computation.py:682–832  ·  view source on GitHub ↗

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, DataArray or Data

(cond, x, y, keep_attrs=None)

Source from the content-addressed store, hash-verified

680
681
682def where(cond, x, y, keep_attrs=None):
683 """Return elements from `x` or `y` depending on `cond`.
684
685 Performs xarray-like broadcasting across input arguments.
686
687 All dimension coordinates on `x` and `y` must be aligned with each
688 other and with `cond`.
689
690 Parameters
691 ----------
692 cond : scalar, array, Variable, DataArray or Dataset
693 When True, return values from `x`, otherwise returns values from `y`.
694 x : scalar, array, Variable, DataArray or Dataset
695 values to choose from where `cond` is True
696 y : scalar, array, Variable, DataArray or Dataset
697 values to choose from where `cond` is False
698 keep_attrs : bool or {"drop", "identical", "no_conflicts", "drop_conflicts", "override"} or callable, optional
699
700 - 'override' or True (default): skip comparing and copy attrs from `x` to the result.
701 - 'drop' or False: empty attrs on returned xarray object.
702 - 'identical': all attrs must be the same on every object.
703 - 'no_conflicts': attrs from all objects are combined, any that have the same name must also have the same value.
704 - 'drop_conflicts': attrs from all objects are combined, any that have the same name but different values are dropped.
705
706 Returns
707 -------
708 Dataset, DataArray, Variable or array
709 In priority order: Dataset, DataArray, Variable or array, whichever
710 type appears as an input argument.
711
712 Examples
713 --------
714 >>> x = xr.DataArray(
715 ... 0.1 * np.arange(10),
716 ... dims=["lat"],
717 ... coords={"lat": np.arange(10)},
718 ... name="sst",
719 ... attrs={"standard_name": "sea_surface_temperature"},
720 ... )
721 >>> x
722 <xarray.DataArray 'sst' (lat: 10)> Size: 80B
723 array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
724 Coordinates:
725 * lat (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
726 Attributes:
727 standard_name: sea_surface_temperature
728
729 >>> xr.where(x < 0.5, x, x * 100)
730 <xarray.DataArray 'sst' (lat: 10)> Size: 80B
731 array([ 0. , 0.1, 0.2, 0.3, 0.4, 50. , 60. , 70. , 80. , 90. ])
732 Coordinates:
733 * lat (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
734 Attributes:
735 standard_name: sea_surface_temperature
736
737 If `x` is a scalar then by default there are no attrs on the result
738
739 >>> xr.where(x < 0.5, 1, 0)

Callers 2

_initialize_feasibleFunction · 0.90

Calls 2

_get_keep_attrsFunction · 0.90
apply_ufuncFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…