MCPcopy
hub / github.com/pydata/xarray / eval

Method eval

xarray/core/dataset.py:9618–9722  ·  view source on GitHub ↗

Calculate an expression supplied as a string in the context of the dataset. This is currently experimental; the API may change particularly around assignments, which currently return a ``Dataset`` with the additional variable. Logical operators (``and``, ``or``, ``

(
        self,
        statement: str,
        *,
        parser: QueryParserOptions | Default = _default,
    )

Source from the content-addressed store, hash-verified

9616 return builtins.eval(code, {"__builtins__": {}}, namespace)
9617
9618 def eval(
9619 self,
9620 statement: str,
9621 *,
9622 parser: QueryParserOptions | Default = _default,
9623 ) -> Self | DataArray:
9624 """
9625 Calculate an expression supplied as a string in the context of the dataset.
9626
9627 This is currently experimental; the API may change particularly around
9628 assignments, which currently return a ``Dataset`` with the additional variable.
9629
9630 Logical operators (``and``, ``or``, ``not``) are automatically transformed
9631 to bitwise operators (``&``, ``|``, ``~``) which work element-wise on arrays.
9632
9633 Parameters
9634 ----------
9635 statement : str
9636 String containing the Python-like expression to evaluate.
9637
9638 Returns
9639 -------
9640 result : Dataset or DataArray, depending on whether ``statement`` contains an
9641 assignment.
9642
9643 Warnings
9644 --------
9645 Like ``pd.eval()``, this method should not be used with untrusted input.
9646
9647 Examples
9648 --------
9649 >>> ds = xr.Dataset(
9650 ... {"a": ("x", np.arange(0, 5, 1)), "b": ("x", np.linspace(0, 1, 5))}
9651 ... )
9652 >>> ds
9653 <xarray.Dataset> Size: 80B
9654 Dimensions: (x: 5)
9655 Dimensions without coordinates: x
9656 Data variables:
9657 a (x) int64 40B 0 1 2 3 4
9658 b (x) float64 40B 0.0 0.25 0.5 0.75 1.0
9659
9660 >>> ds.eval("a + b")
9661 <xarray.DataArray (x: 5)> Size: 40B
9662 array([0. , 1.25, 2.5 , 3.75, 5. ])
9663 Dimensions without coordinates: x
9664
9665 >>> ds.eval("c = a + b")
9666 <xarray.Dataset> Size: 120B
9667 Dimensions: (x: 5)
9668 Dimensions without coordinates: x
9669 Data variables:
9670 a (x) int64 40B 0 1 2 3 4
9671 b (x) float64 40B 0.0 0.25 0.5 0.75 1.0
9672 c (x) float64 40B 0.0 1.25 2.5 3.75 5.0
9673 """
9674 if parser is not _default:
9675 emit_user_level_warning(

Calls 5

_eval_expressionMethod · 0.95
assignMethod · 0.95
emit_user_level_warningFunction · 0.90
typeFunction · 0.85
stripMethod · 0.80