Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Dec 3, 2023
1 parent d1672c7 commit 875274d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 69 deletions.
8 changes: 4 additions & 4 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ New Features
- :py:meth:`~xarray.DataArray.rank` now operates on dask-backed arrays, assuming
the core dim has exactly one chunk. (:pull:`8475`).
By `Maximilian Roos <https://github.com/max-sixty>`_.
- Add a :py:meth:`Dataset.eval` method, similar to the pandas' method of the
same name. (:pull:`7163`). This is currently marked as experimental and
doesn't yet support the ``numexpr`` engine.
By `Maximilian Roos <https://github.com/max-sixty>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -124,10 +128,6 @@ New Features
By `Sam Levang <https://github.com/slevang>`_.
- Allow the usage of h5py drivers (eg: ros3) via h5netcdf (:pull:`8360`).
By `Ezequiel Cimadevilla <https://github.com/zequihg50>`_.
- Add a :py:meth:`Dataset.eval` method, similar to the pandas' method of the
same name. (:pull:`7163`). This is currently marked as experimental and
doesn't yet support the ``numexpr`` engine.
By `Maximilian Roos <https://github.com/max-sixty>`_.
- Enable VLEN string fill_values, preserve VLEN string dtypes (:issue:`1647`, :issue:`7652`, :issue:`7868`, :pull:`7869`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

Expand Down
79 changes: 14 additions & 65 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6691,76 +6691,25 @@ def test_query(self, backend, engine, parser) -> None:
with pytest.raises(UndefinedVariableError):
ds.query(x="spam > 50") # name not present

@pytest.mark.parametrize("parser", ["pandas", "python"])
@pytest.mark.parametrize(
"backend", ["numpy", pytest.param("dask", marks=[requires_dask])]
)
def test_eval(self, backend, parser) -> None:
"""Currently much more minimal testing that `query` above, and much of the setup
isn't used. But the risks are fairly low — `query` shares much of the code, and
the method is currently experimental."""

# setup test data
np.random.seed(42)
a = np.arange(0, 10, 1)
b = np.random.randint(0, 100, size=10)
c = np.linspace(0, 1, 20)
d = np.random.choice(["foo", "bar", "baz"], size=30, replace=True).astype(
object
)
e = np.arange(0, 10 * 20).reshape(10, 20)
f = np.random.normal(0, 1, size=(10, 20, 30))
if backend == "numpy":
ds = Dataset(
{
"a": ("x", a),
"b": ("x", b),
"c": ("y", c),
"d": ("z", d),
"e": (("x", "y"), e),
"f": (("x", "y", "z"), f),
},
coords={
"a2": ("x", a),
"b2": ("x", b),
"c2": ("y", c),
"d2": ("z", d),
"e2": (("x", "y"), e),
"f2": (("x", "y", "z"), f),
},
)
elif backend == "dask":
ds = Dataset(
{
"a": ("x", da.from_array(a, chunks=3)),
"b": ("x", da.from_array(b, chunks=3)),
"c": ("y", da.from_array(c, chunks=7)),
"d": ("z", da.from_array(d, chunks=12)),
"e": (("x", "y"), da.from_array(e, chunks=(3, 7))),
"f": (("x", "y", "z"), da.from_array(f, chunks=(3, 7, 12))),
},
coords={
"a2": ("x", a),
"b2": ("x", b),
"c2": ("y", c),
"d2": ("z", d),
"e2": (("x", "y"), e),
"f2": (("x", "y", "z"), f),
},
)
# pytest tests — new tests should go here, rather than in the class.

actual = ds.eval("a + 5", parser=parser)
expect = ds["a"] + 5
assert_identical(expect, actual)

# check pandas query syntax is supported
if parser == "pandas":
actual = ds.eval("(a2 > 5) and (b2 > 50)", parser=parser)
expect = (ds["a"] > 5) & (ds["b"] > 50)
assert_identical(expect, actual)
@pytest.mark.parametrize("parser", ["pandas", "python"])
def test_eval(ds, parser) -> None:
"""Currently much more minimal testing that `query` above, and much of the setup
isn't used. But the risks are fairly low — `query` shares much of the code, and
the method is currently experimental."""

actual = ds.eval("z1 + 5", parser=parser)
expect = ds["z1"] + 5
assert_identical(expect, actual)

# pytest tests — new tests should go here, rather than in the class.
# check pandas query syntax is supported
if parser == "pandas":
actual = ds.eval("(z1 > 5) and (z2 > 0)", parser=parser)
expect = (ds["z1"] > 5) & (ds["z2"] > 0)
assert_identical(expect, actual)


@pytest.mark.parametrize("test_elements", ([1, 2], np.array([1, 2]), DataArray([1, 2])))
Expand Down

0 comments on commit 875274d

Please sign in to comment.