question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

combine_by_coords fails with DataArrays

See original GitHub issue

MCVE 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:closed
  • Created 4 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
dcheriancommented, Sep 16, 2019

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 of Datasets or DataArrays. See this code in merge: https://github.com/pydata/xarray/blob/756c94164840e8c070bcd26681b97c31412909ae/xarray/core/merge.py#L593-L602

to_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?

0reactions
aijamscommented, Feb 10, 2021

It looks like this issue has been neglected for a while. @TomNicholas, can you take a look at this PR?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found