Skip to content

Commit

Permalink
Update tests and add new ones to exercise options
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell committed Feb 26, 2025
1 parent 0026ee8 commit 4d4deda
Show file tree
Hide file tree
Showing 8 changed files with 581 additions and 144 deletions.
277 changes: 209 additions & 68 deletions xarray/tests/test_backends.py

Large diffs are not rendered by default.

238 changes: 206 additions & 32 deletions xarray/tests/test_combine.py

Large diffs are not rendered by default.

26 changes: 21 additions & 5 deletions xarray/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,11 @@ def test_concat_loads_variables(self):

assert kernel_call_count == 0
out = xr.concat(
[ds1, ds2, ds3], dim="n", data_vars="different", coords="different"
[ds1, ds2, ds3],
dim="n",
data_vars="different",
coords="different",
compat="equals",
)
# each kernel is computed exactly once
assert kernel_call_count == 6
Expand Down Expand Up @@ -488,7 +492,11 @@ def test_concat_loads_variables(self):
# stop computing variables as it would not have any benefit
ds4 = Dataset(data_vars={"d": ("x", [2.0])}, coords={"c": ("x", [2.0])})
out = xr.concat(
[ds1, ds2, ds4, ds3], dim="n", data_vars="different", coords="different"
[ds1, ds2, ds4, ds3],
dim="n",
data_vars="different",
coords="different",
compat="equals",
)
# the variables of ds1 and ds2 were computed, but those of ds3 didn't
assert kernel_call_count == 22
Expand All @@ -509,7 +517,11 @@ def test_concat_loads_variables(self):

# now check that concat() is correctly using dask name equality to skip loads
out = xr.concat(
[ds1, ds1, ds1], dim="n", data_vars="different", coords="different"
[ds1, ds1, ds1],
dim="n",
data_vars="different",
coords="different",
compat="equals",
)
assert kernel_call_count == 24
# variables are not loaded in the output
Expand Down Expand Up @@ -1375,7 +1387,9 @@ def test_map_blocks_ds_transformations(func, map_ds):
def test_map_blocks_da_ds_with_template(obj):
func = lambda x: x.isel(x=[1])
# a simple .isel(x=[1, 5, 9]) puts all those in a single chunk.
template = xr.concat([obj.isel(x=[i]) for i in [1, 5, 9]], dim="x")
template = xr.concat(
[obj.isel(x=[i]) for i in [1, 5, 9]], data_vars="minimal", dim="x"
)
with raise_if_dask_computes():
actual = xr.map_blocks(func, obj, template=template)
assert_identical(actual, template)
Expand Down Expand Up @@ -1448,7 +1462,9 @@ def test_map_blocks_errors_bad_template(obj):
xr.map_blocks(
lambda a: a.isel(x=[1]).assign_coords(x=[120]), # assign bad index values
obj,
template=xr.concat([obj.isel(x=[i]) for i in [1, 5, 9]], dim="x"),
template=xr.concat(
[obj.isel(x=[i]) for i in [1, 5, 9]], data_vars="minimal", dim="x"
),
).compute()


Expand Down
15 changes: 14 additions & 1 deletion xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,12 +1412,25 @@ def test_selection_multiindex_from_level(self) -> None:
# GH: 3512
da = DataArray([0, 1], dims=["x"], coords={"x": [0, 1], "y": "a"})
db = DataArray([2, 3], dims=["x"], coords={"x": [0, 1], "y": "b"})
data = xr.concat([da, db], dim="x").set_index(xy=["x", "y"])
data = xr.concat(
[da, db], dim="x", coords="different", compat="equals"
).set_index(xy=["x", "y"])
assert data.dims == ("xy",)
actual = data.sel(y="a")
expected = data.isel(xy=[0, 1]).unstack("xy").squeeze("y")
assert_equal(actual, expected)

def test_concat_with_default_coords_warns(self) -> None:
da = DataArray([0, 1], dims=["x"], coords={"x": [0, 1], "y": "a"})
db = DataArray([2, 3], dims=["x"], coords={"x": [0, 1], "y": "b"})

with pytest.warns(FutureWarning):
original = xr.concat([da, db], dim="x")
with set_options(use_new_combine_kwarg_defaults=True):
new = xr.concat([da, db], dim="x")

assert original.y.shape != new.y.shape

def test_virtual_default_coords(self) -> None:
array = DataArray(np.zeros((5,)), dims="x")
expected = DataArray(range(5), dims="x", name="x")
Expand Down
6 changes: 3 additions & 3 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6085,7 +6085,7 @@ def test_dataset_math_auto_align(self) -> None:
assert_equal(actual, expected)

actual = ds + ds[["bar"]]
expected = (2 * ds[["bar"]]).merge(ds.coords)
expected = (2 * ds[["bar"]]).merge(ds.coords, compat="override")
assert_identical(expected, actual)

assert_identical(ds + Dataset(), ds.coords.to_dataset())
Expand Down Expand Up @@ -6521,12 +6521,12 @@ def test_combine_first(self) -> None:
coords={"x": ["a", "b", "c"]},
)
assert_equal(actual, expected)
assert_equal(actual, xr.merge([dsx0, dsx1]))
assert_equal(actual, xr.merge([dsx0, dsx1], join="outer"))

# works just like xr.merge([self, other])
dsy2 = DataArray([2, 2, 2], [("x", ["b", "c", "d"])]).to_dataset(name="dsy2")
actual = dsx0.combine_first(dsy2)
expected = xr.merge([dsy2, dsx0])
expected = xr.merge([dsy2, dsx0], join="outer")
assert_equal(actual, expected)

def test_sortby(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_duck_array_wrapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_concat(self):
assert isinstance(result.data, self.Array)

def test_merge(self):
result = xr.merge([self.x1, self.x2], compat="override")
result = xr.merge([self.x1, self.x2], compat="override", join="outer")
assert isinstance(result.foo.data, self.Array)

def test_where(self):
Expand Down
1 change: 1 addition & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,7 @@ def test_resample_min_count(self) -> None:
for i in range(3)
],
dim=actual["time"],
data_vars="all",
)
assert_allclose(expected, actual)

Expand Down
Loading

0 comments on commit 4d4deda

Please sign in to comment.