diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b01c53e76e2..be91d95a54d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -48,6 +48,8 @@ Enhancements helpful for avoiding file-lock errors when trying to write to files opened using ``open_dataset()`` or ``open_dataarray()``. (:issue:`2887`) By `Dan Nowacki `_. +- Better warning message when supplying invalid objects to ``xr.merge`` + (:issue:`2948`). By `Mathias Hauser `_. - Added ``strftime`` method to ``.dt`` accessor, making it simpler to hand a datetime ``DataArray`` to other code expecting formatted dates and times. (:issue:`2090`). By `Alan Brammer `_ and diff --git a/xarray/core/merge.py b/xarray/core/merge.py index 421ac39ebd8..c2c6aee7c22 100644 --- a/xarray/core/merge.py +++ b/xarray/core/merge.py @@ -533,9 +533,14 @@ def merge(objects, compat='no_conflicts', join='outer', fill_value=dtypes.NA): from .dataarray import DataArray from .dataset import Dataset - dict_like_objects = [ - obj.to_dataset() if isinstance(obj, DataArray) else obj - for obj in objects] + dict_like_objects = list() + for obj in objects: + if not (isinstance(obj, (DataArray, Dataset, dict))): + raise TypeError("objects must be an iterable containing only " + "Dataset(s), DataArray(s), and dictionaries.") + + obj = obj.to_dataset() if isinstance(obj, DataArray) else obj + dict_like_objects.append(obj) variables, coord_names, dims = merge_core(dict_like_objects, compat, join, fill_value=fill_value) diff --git a/xarray/tests/test_merge.py b/xarray/tests/test_merge.py index c45195eaa7f..20e0fae8daf 100644 --- a/xarray/tests/test_merge.py +++ b/xarray/tests/test_merge.py @@ -67,6 +67,15 @@ def test_merge_alignment_error(self): with raises_regex(ValueError, 'indexes .* not equal'): xr.merge([ds, other], join='exact') + def test_merge_wrong_input_error(self): + with raises_regex(TypeError, "objects must be an iterable"): + xr.merge([1]) + ds = xr.Dataset(coords={'x': [1, 2]}) + with raises_regex(TypeError, "objects must be an iterable"): + xr.merge({'a': ds}) + with raises_regex(TypeError, "objects must be an iterable"): + xr.merge([ds, 1]) + def test_merge_no_conflicts_single_var(self): ds1 = xr.Dataset({'a': ('x', [1, 2]), 'x': [0, 1]}) ds2 = xr.Dataset({'a': ('x', [2, 3]), 'x': [1, 2]})