Skip to content

Commit 1689db4

Browse files
authored
ds.merge(da) bugfix (#3677)
* Added mwe as test * Cast to Dataset * Updated what's new * black formatted * Use assert_identical
1 parent 40fab1b commit 1689db4

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Bug fixes
7474
By `Justus Magin <https://github.com/keewis>`_.
7575
- :py:meth:`Dataset.rename`, :py:meth:`DataArray.rename` now check for conflicts with
7676
MultiIndex level names.
77+
- :py:meth:`Dataset.merge` no longer fails when passed a `DataArray` instead of a `Dataset` object.
78+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
7779

7880
Documentation
7981
~~~~~~~~~~~~~

xarray/core/dataset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3607,6 +3607,7 @@ def merge(
36073607
If any variables conflict (see ``compat``).
36083608
"""
36093609
_check_inplace(inplace)
3610+
other = other.to_dataset() if isinstance(other, xr.DataArray) else other
36103611
merge_result = dataset_merge_method(
36113612
self,
36123613
other,

xarray/tests/test_merge.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import xarray as xr
55
from xarray.core import dtypes, merge
6+
from xarray.testing import assert_identical
67

78
from . import raises_regex
89
from .test_dataset import create_test_data
@@ -253,3 +254,9 @@ def test_merge_no_conflicts(self):
253254
with pytest.raises(xr.MergeError):
254255
ds3 = xr.Dataset({"a": ("y", [2, 3]), "y": [1, 2]})
255256
ds1.merge(ds3, compat="no_conflicts")
257+
258+
def test_merge_dataarray(self):
259+
ds = xr.Dataset({"a": 0})
260+
da = xr.DataArray(data=1, name="b")
261+
262+
assert_identical(ds.merge(da), xr.merge([ds, da]))

0 commit comments

Comments
 (0)