|
98 | 98 | Self, |
99 | 99 | T_ChunkDim, |
100 | 100 | T_Chunks, |
| 101 | + T_DataArray, |
101 | 102 | T_DataArrayOrSet, |
102 | 103 | T_Dataset, |
103 | 104 | ZarrWriteModes, |
@@ -9554,6 +9555,68 @@ def argmax(self, dim: Hashable | None = None, **kwargs) -> Self: |
9554 | 9555 | "Dataset.argmin() with a sequence or ... for dim" |
9555 | 9556 | ) |
9556 | 9557 |
|
| 9558 | + def eval( |
| 9559 | + self, |
| 9560 | + statement: str, |
| 9561 | + *, |
| 9562 | + parser: QueryParserOptions = "pandas", |
| 9563 | + ) -> Self | T_DataArray: |
| 9564 | + """ |
| 9565 | + Calculate an expression supplied as a string in the context of the dataset. |
| 9566 | +
|
| 9567 | + This is currently experimental; the API may change particularly around |
| 9568 | + assignments, which currently returnn a ``Dataset`` with the additional variable. |
| 9569 | + Currently only the ``python`` engine is supported, which has the same |
| 9570 | + performance as executing in python. |
| 9571 | +
|
| 9572 | + Parameters |
| 9573 | + ---------- |
| 9574 | + statement : str |
| 9575 | + String containing the Python-like expression to evaluate. |
| 9576 | +
|
| 9577 | + Returns |
| 9578 | + ------- |
| 9579 | + result : Dataset or DataArray, depending on whether ``statement`` contains an |
| 9580 | + assignment. |
| 9581 | +
|
| 9582 | + Examples |
| 9583 | + -------- |
| 9584 | + >>> ds = xr.Dataset( |
| 9585 | + ... {"a": ("x", np.arange(0, 5, 1)), "b": ("x", np.linspace(0, 1, 5))} |
| 9586 | + ... ) |
| 9587 | + >>> ds |
| 9588 | + <xarray.Dataset> |
| 9589 | + Dimensions: (x: 5) |
| 9590 | + Dimensions without coordinates: x |
| 9591 | + Data variables: |
| 9592 | + a (x) int64 0 1 2 3 4 |
| 9593 | + b (x) float64 0.0 0.25 0.5 0.75 1.0 |
| 9594 | +
|
| 9595 | + >>> ds.eval("a + b") |
| 9596 | + <xarray.DataArray (x: 5)> |
| 9597 | + array([0. , 1.25, 2.5 , 3.75, 5. ]) |
| 9598 | + Dimensions without coordinates: x |
| 9599 | +
|
| 9600 | + >>> ds.eval("c = a + b") |
| 9601 | + <xarray.Dataset> |
| 9602 | + Dimensions: (x: 5) |
| 9603 | + Dimensions without coordinates: x |
| 9604 | + Data variables: |
| 9605 | + a (x) int64 0 1 2 3 4 |
| 9606 | + b (x) float64 0.0 0.25 0.5 0.75 1.0 |
| 9607 | + c (x) float64 0.0 1.25 2.5 3.75 5.0 |
| 9608 | + """ |
| 9609 | + |
| 9610 | + return pd.eval( |
| 9611 | + statement, |
| 9612 | + resolvers=[self], |
| 9613 | + target=self, |
| 9614 | + parser=parser, |
| 9615 | + # Because numexpr returns a numpy array, using that engine results in |
| 9616 | + # different behavior. We'd be very open to a contribution handling this. |
| 9617 | + engine="python", |
| 9618 | + ) |
| 9619 | + |
9557 | 9620 | def query( |
9558 | 9621 | self, |
9559 | 9622 | queries: Mapping[Any, Any] | None = None, |
|
0 commit comments