Skip to content

Commit

Permalink
Add regression test for Dataset (pydata#7030)
Browse files Browse the repository at this point in the history
  • Loading branch information
dranjan committed Aug 18, 2023
1 parent e152e4b commit f433db6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ def get_data():
da[dict(x=ind)] = value # should not raise

def test_setitem_vectorized(self) -> None:
# Regression test for GH:7030
# Positional indexing
v = xr.DataArray(np.r_[:120].reshape(2, 3, 4, 5), dims=["a", "b", "c", "d"])
b = xr.DataArray([[0, 0], [1, 0]], dims=["u", "v"])
Expand Down
23 changes: 23 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4160,6 +4160,29 @@ def test_setitem_align_new_indexes(self) -> None:
)
assert_identical(ds, expected)

def test_setitem_vectorized(self) -> None:
# Regression test for GH:7030
# Positional indexing
da = xr.DataArray(np.r_[:120].reshape(2, 3, 4, 5), dims=["a", "b", "c", "d"])
ds = xr.Dataset({'da': da})
b = xr.DataArray([[0, 0], [1, 0]], dims=["u", "v"])
c = xr.DataArray([[0, 1], [2, 3]], dims=["u", "v"])
w = xr.DataArray([-1, -2], dims=["u"])
index = dict(b=b, c=c)
ds[index] = xr.Dataset({'da': w})
assert (ds[index]['da'] == w).all()

# Indexing with coordinates
da = xr.DataArray(np.r_[:120].reshape(2, 3, 4, 5), dims=["a", "b", "c", "d"])
ds = xr.Dataset({'da': da})
ds.coords["b"] = [2, 4, 6]
b = xr.DataArray([[2, 2], [4, 2]], dims=["u", "v"])
c = xr.DataArray([[0, 1], [2, 3]], dims=["u", "v"])
w = xr.DataArray([-1, -2], dims=["u"])
index = dict(b=b, c=c)
ds.loc[index] = xr.Dataset({'da': w}, coords={'b': ds.coords['b']})
assert (ds.loc[index]['da'] == w).all()

@pytest.mark.parametrize("dtype", [str, bytes])
def test_setitem_str_dtype(self, dtype) -> None:
ds = xr.Dataset(coords={"x": np.array(["x", "y"], dtype=dtype)})
Expand Down

0 comments on commit f433db6

Please sign in to comment.