cf.Data.where¶

Data.
where
(condition, x=None, y=None, inplace=False, i=False, _debug=False)[source]¶ Assign to data elements depending on a condition.
Data can be changed by assigning to elements that are selected by a condition based on the data values.
Different values can be assigned to where the conditions are, and are not, met.
Missing data
Data array elements may be set to missing values by assigning them to the cf.masked constant, or by assignment missing data elements of arrayvalued x and y parameters.
By default the data mask is “hard”, meaning that masked values can not be changed by assigning them to another value. This behaviour may be changed by setting the
hardmask
attribute toFalse
, thereby making the data mask “soft” and allowing masked elements to be set to nonmasked values.See also
Parameters:  condition:
The condition which determines how to assign values to the data.
In general it may be any scalar or arraylike object (such as a numpy array or
Data
instance) that is broadcastable to the shape of the data. Assignment from the x and y parameters will be done where elements of the condition evaluate toTrue
andFalse
respectively. Parameter example:
d.where(d.data<0, x=999)
will set all data values that are less than zero to 999. Parameter example:
d.where(True, x=999)
will set all data values to 999. This is equivalent tod[...] = 999
. Parameter example:
d.where(False, y=999)
will set all data values to 999. This is equivalent tod[...] = 999
. Parameter example:
If data
d
has shape(5, 3)
thend.where([True, False, True], x=999, y=cf.masked)
will set data values in columns 0 and 2 to 999, and data values in column 1 to missing data. This works because the condition has shape(3,)
which broadcasts to the data shape.
If condition is a
Query
object then this implies a condition defined by applying the query to the data. Parameter example:
d.where(cf.lt(0), x=999)
will set all data values that are less than zero to 999. This is equivalent tod.where(d<0, x=999)
.
 x, y: optional
Specify the assignment values. Where the condition evaluates to
True
, assign to the data from x, and where the condition evaluates toFalse
, assign to the data from y. The x and y parameters are each one of:
 Parameter example:
d.where(condition)
, for anycondition
, returns data with identical data values. Parameter example:
d.where(cf.lt(0), x=d, y=cf.masked)
will change the sign of all negative data values, and set all other data values to missing data.
Returns: Examples: