query

UnitsAwareDataArray.query(queries: Mapping[Any, Any] | None = None, parser: QueryParserOptions = 'pandas', engine: QueryEngineOptions = None, missing_dims: ErrorOptionsWithWarn = 'raise', **queries_kwargs: Any) DataArray

Return a new data array indexed along the specified dimension(s), where the indexers are given as strings containing Python expressions to be evaluated against the values in the array.

Parameters:
  • queries (dict-like or None, optional) – A dict-like with keys matching dimensions and values given by strings containing Python expressions to be evaluated against the data variables in the dataset. The expressions will be evaluated using the pandas eval() function, and can contain any valid Python expressions but cannot contain any Python statements.

  • parser ({"pandas", "python"}, default: "pandas") – The parser to use to construct the syntax tree from the expression. The default of ‘pandas’ parses code slightly different than standard Python. Alternatively, you can parse an expression using the ‘python’ parser to retain strict Python semantics.

  • engine ({"python", "numexpr", None}, default: None) –

    The engine used to evaluate the expression. Supported engines are:

    • None: tries to use numexpr, falls back to python

    • ”numexpr”: evaluates expressions using numexpr

    • ”python”: performs operations as if you had eval’d in top level python

  • missing_dims ({"raise", "warn", "ignore"}, default: "raise") –

    What to do if dimensions that should be selected from are not present in the DataArray:

    • ”raise”: raise an exception

    • ”warn”: raise a warning, and ignore the missing dimensions

    • ”ignore”: ignore the missing dimensions

  • **queries_kwargs ({dim: query, ...}, optional) – The keyword arguments form of queries. One of queries or queries_kwargs must be provided.

Returns:

obj – A new DataArray with the same contents as this dataset, indexed by the results of the appropriate queries.

Return type:

DataArray

See also

DataArray.isel, Dataset.query, pandas.eval

Examples

>>> da = xr.DataArray(np.arange(0, 5, 1), dims="x", name="a")
>>> da
<xarray.DataArray 'a' (x: 5)> Size: 40B
array([0, 1, 2, 3, 4])
Dimensions without coordinates: x
>>> da.query(x="a > 2")
<xarray.DataArray 'a' (x: 2)> Size: 16B
array([3, 4])
Dimensions without coordinates: x