Skip to content

Commit 8215911

Browse files
dranjanpre-commit-ci[bot]max-sixty
authored
Fix assignment with .loc (#8067)
* Add unit test for Variable[...] = DataArray (#7030) * Check for DataArray instance (#7030) * Update whats-new.rst (#7030) * Revert Variable and fix DataArray instead (#7030) * Add test case for .loc[...] = dataarray (#7030) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix whats-new.rst (#7030) * Add regression test for Dataset (#7030) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maximilian Roos <[email protected]>
1 parent de66dae commit 8215911

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Bug fixes
6060
names were not updated properly internally (:issue:`7405`, :issue:`7588`,
6161
:pull:`8104`).
6262
By `Benoît Bovy <https://github.com/benbovy>`_.
63+
- Fix bug where :py:class:`DataArray` instances on the right-hand side
64+
of :py:meth:`DataArray.__setitem__` lose dimension names.
65+
(:issue:`7030`, :pull:`8067`) By `Darsh Ranjan <https://github.com/dranjan>`_.
6366

6467
Documentation
6568
~~~~~~~~~~~~~

xarray/core/dataarray.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ def __setitem__(self, key: Any, value: Any) -> None:
856856
obj = self[key]
857857
if isinstance(value, DataArray):
858858
assert_coordinate_consistent(value, obj.coords.variables)
859+
value = value.variable
859860
# DataArray key -> Variable key
860861
key = {
861862
k: v.variable if isinstance(v, DataArray) else v

xarray/tests/test_dataarray.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,27 @@ def get_data():
841841
)
842842
da[dict(x=ind)] = value # should not raise
843843

844+
def test_setitem_vectorized(self) -> None:
845+
# Regression test for GH:7030
846+
# Positional indexing
847+
v = xr.DataArray(np.r_[:120].reshape(2, 3, 4, 5), dims=["a", "b", "c", "d"])
848+
b = xr.DataArray([[0, 0], [1, 0]], dims=["u", "v"])
849+
c = xr.DataArray([[0, 1], [2, 3]], dims=["u", "v"])
850+
w = xr.DataArray([-1, -2], dims=["u"])
851+
index = dict(b=b, c=c)
852+
v[index] = w
853+
assert (v[index] == w).all()
854+
855+
# Indexing with coordinates
856+
v = xr.DataArray(np.r_[:120].reshape(2, 3, 4, 5), dims=["a", "b", "c", "d"])
857+
v.coords["b"] = [2, 4, 6]
858+
b = xr.DataArray([[2, 2], [4, 2]], dims=["u", "v"])
859+
c = xr.DataArray([[0, 1], [2, 3]], dims=["u", "v"])
860+
w = xr.DataArray([-1, -2], dims=["u"])
861+
index = dict(b=b, c=c)
862+
v.loc[index] = w
863+
assert (v.loc[index] == w).all()
864+
844865
def test_contains(self) -> None:
845866
data_array = DataArray([1, 2])
846867
assert 1 in data_array

xarray/tests/test_dataset.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,6 +4202,29 @@ def test_setitem_align_new_indexes(self) -> None:
42024202
)
42034203
assert_identical(ds, expected)
42044204

4205+
def test_setitem_vectorized(self) -> None:
4206+
# Regression test for GH:7030
4207+
# Positional indexing
4208+
da = xr.DataArray(np.r_[:120].reshape(2, 3, 4, 5), dims=["a", "b", "c", "d"])
4209+
ds = xr.Dataset({"da": da})
4210+
b = xr.DataArray([[0, 0], [1, 0]], dims=["u", "v"])
4211+
c = xr.DataArray([[0, 1], [2, 3]], dims=["u", "v"])
4212+
w = xr.DataArray([-1, -2], dims=["u"])
4213+
index = dict(b=b, c=c)
4214+
ds[index] = xr.Dataset({"da": w})
4215+
assert (ds[index]["da"] == w).all()
4216+
4217+
# Indexing with coordinates
4218+
da = xr.DataArray(np.r_[:120].reshape(2, 3, 4, 5), dims=["a", "b", "c", "d"])
4219+
ds = xr.Dataset({"da": da})
4220+
ds.coords["b"] = [2, 4, 6]
4221+
b = xr.DataArray([[2, 2], [4, 2]], dims=["u", "v"])
4222+
c = xr.DataArray([[0, 1], [2, 3]], dims=["u", "v"])
4223+
w = xr.DataArray([-1, -2], dims=["u"])
4224+
index = dict(b=b, c=c)
4225+
ds.loc[index] = xr.Dataset({"da": w}, coords={"b": ds.coords["b"]})
4226+
assert (ds.loc[index]["da"] == w).all()
4227+
42054228
@pytest.mark.parametrize("dtype", [str, bytes])
42064229
def test_setitem_str_dtype(self, dtype) -> None:
42074230
ds = xr.Dataset(coords={"x": np.array(["x", "y"], dtype=dtype)})

0 commit comments

Comments
 (0)