combine_by_coords fails with DataArrays
See original GitHub issueMCVE Code Sample
da1 = xr.DataArray([1, 2, 3], dims='x', coords={'x': [0, 1, 2]})
da2 = xr.DataArray([3, 4, 5], dims='x', coords={'x': [2, 3, 4]})
xr.combine_by_coords([da1, da2])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-133-d27216ba5688> in <module>
1 da1 = xr.DataArray([1, 2, 3], dims='x', coords={'x': [0, 1, 2]})
2 da2 = xr.DataArray([3, 4, 5], dims='x', coords={'x': [2, 3, 4]})
----> 3 xr.combine_by_coords([da1, da2])
~/work/python/xarray/xarray/core/combine.py in combine_by_coords(datasets, compat, data_vars, coords, fill_value, join)
619 compat=compat,
620 fill_value=fill_value,
--> 621 join=join,
622 )
623
~/work/python/xarray/xarray/core/merge.py in merge(objects, compat, join, fill_value)
588 )
589
--> 590 obj = obj.to_dataset() if isinstance(obj, DataArray) else obj
591 dict_like_objects.append(obj)
592
~/work/python/xarray/xarray/core/dataarray.py in to_dataset(self, dim, name)
478 return self._to_dataset_split(dim)
479 else:
--> 480 return self._to_dataset_whole(name)
481
482 @property
~/work/python/xarray/xarray/core/dataarray.py in _to_dataset_whole(self, name, shallow_copy)
426 if name is None:
427 raise ValueError(
--> 428 "unable to convert unnamed DataArray to a "
429 "Dataset without providing an explicit name"
430 )
ValueError: unable to convert unnamed DataArray to a Dataset without providing an explicit name
To get what I want, I need
xr.combine_by_coords([da1.to_dataset(name='a'), da2.to_dataset(name='a')]).a
I think the issue is that the code uses to_dataset
instead of _to_temp_dataset
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (9 by maintainers)
Top Results From Across the Web
Xarray combine_by_coords return the monotonic global index ...
However, it returns the monotonic global indexes along dimension y error. It looks like it was an issue before but it was fixed...
Read more >xarray.combine_by_coords
Aligns coordinates, but different variables on datasets can cause it to fail under some scenarios. In complex cases, you may need to clean...
Read more >Combining data - xarray - Read the Docs
For combining datasets with different variables, see merge. For combining datasets or data arrays with different indexes or missing values, see combine.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @friedrichknuth .
It looks like all the
combine
functions expect lists of Datasets, not DataArrays (see docstrings).It should be relatively easily to make this work like
merge
which can take a list ofDatasets or DataArrays
. See this code inmerge
: https://github.com/pydata/xarray/blob/756c94164840e8c070bcd26681b97c31412909ae/xarray/core/merge.py#L593-L602to_dataset
will fail for unnamed DataArrays, but_to_temp_dataset()
will succeed. It seems to me like we want lists of unnamed DataArrays to work (like in the first example), so call_to_temp_dataset
when needed. Maybe @shoyer has an idea on how to implement that cleanly.Can you send in a PR?
It looks like this issue has been neglected for a while. @TomNicholas, can you take a look at this PR?