Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/merge errormsg #2971

Merged
merged 8 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/dnowacki-usgs>`_.
- Better warning message when supplying invalid objects to ``xr.merge``
(:issue:`2948`). By `Mathias Hauser <https://github.com/mathause>`_.
- 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 <https://github.com/abrammer>`_ and
Expand Down
11 changes: 8 additions & 3 deletions xarray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down