Filter elements from this object according to a condition. Returns elements from 'DataArray', where 'cond' is True, otherwise fill in 'other'. This operation follows the normal broadcasting and alignment rules that xarray uses for binary arithmetic. Paramet
(self, cond: Any, other: Any = dtypes.NA, drop: bool = False)
| 1129 | ) |
| 1130 | |
| 1131 | def where(self, cond: Any, other: Any = dtypes.NA, drop: bool = False) -> Self: |
| 1132 | """Filter elements from this object according to a condition. |
| 1133 | |
| 1134 | Returns elements from 'DataArray', where 'cond' is True, |
| 1135 | otherwise fill in 'other'. |
| 1136 | |
| 1137 | This operation follows the normal broadcasting and alignment rules that |
| 1138 | xarray uses for binary arithmetic. |
| 1139 | |
| 1140 | Parameters |
| 1141 | ---------- |
| 1142 | cond : DataArray, Dataset, or callable |
| 1143 | Locations at which to preserve this object's values. dtype must be `bool`. |
| 1144 | If a callable, the callable is passed this object, and the result is used as |
| 1145 | the value for cond. |
| 1146 | other : scalar, DataArray, Dataset, or callable, optional |
| 1147 | Value to use for locations in this object where ``cond`` is False. |
| 1148 | By default, these locations are filled with NA. If a callable, it must |
| 1149 | expect this object as its only parameter. |
| 1150 | drop : bool, default: False |
| 1151 | If True, coordinate labels that only correspond to False values of |
| 1152 | the condition are dropped from the result. |
| 1153 | |
| 1154 | Returns |
| 1155 | ------- |
| 1156 | DataArray or Dataset |
| 1157 | Same xarray type as caller, with dtype float64. |
| 1158 | |
| 1159 | Examples |
| 1160 | -------- |
| 1161 | >>> a = xr.DataArray(np.arange(25).reshape(5, 5), dims=("x", "y")) |
| 1162 | >>> a |
| 1163 | <xarray.DataArray (x: 5, y: 5)> Size: 200B |
| 1164 | array([[ 0, 1, 2, 3, 4], |
| 1165 | [ 5, 6, 7, 8, 9], |
| 1166 | [10, 11, 12, 13, 14], |
| 1167 | [15, 16, 17, 18, 19], |
| 1168 | [20, 21, 22, 23, 24]]) |
| 1169 | Dimensions without coordinates: x, y |
| 1170 | |
| 1171 | >>> a.where(a.x + a.y < 4) |
| 1172 | <xarray.DataArray (x: 5, y: 5)> Size: 200B |
| 1173 | array([[ 0., 1., 2., 3., nan], |
| 1174 | [ 5., 6., 7., nan, nan], |
| 1175 | [10., 11., nan, nan, nan], |
| 1176 | [15., nan, nan, nan, nan], |
| 1177 | [nan, nan, nan, nan, nan]]) |
| 1178 | Dimensions without coordinates: x, y |
| 1179 | |
| 1180 | >>> a.where(a.x + a.y < 5, -1) |
| 1181 | <xarray.DataArray (x: 5, y: 5)> Size: 200B |
| 1182 | array([[ 0, 1, 2, 3, 4], |
| 1183 | [ 5, 6, 7, 8, -1], |
| 1184 | [10, 11, 12, -1, -1], |
| 1185 | [15, 16, -1, -1, -1], |
| 1186 | [20, -1, -1, -1, -1]]) |
| 1187 | Dimensions without coordinates: x, y |
| 1188 |