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

convert DataArray to DataSet before combine #3312

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
25 changes: 21 additions & 4 deletions xarray/core/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def vars_as_keys(ds):


def combine_by_coords(
datasets,
objects,
compat="no_conflicts",
data_vars="all",
coords="different",
Expand All @@ -483,7 +483,9 @@ def combine_by_coords(

This method attempts to combine a group of datasets along any number of
dimensions into a single entity by inspecting coords and metadata and using
a combination of concat and merge.
a combination of concat and merge. If passed a DataArray, the object
will be converted to Dataset before being combined with the remaining datasets
in the iterable.

Will attempt to order the datasets such that the values in their dimension
coordinates are monotonic along all dimensions. If it cannot determine the
Expand All @@ -503,8 +505,7 @@ def combine_by_coords(

Parameters
----------
datasets : sequence of xarray.Dataset
Dataset objects to combine.
objects : sequence of xarray.Dataset or xarray.DataArray objects
compat : {'identical', 'equals', 'broadcast_equals',
'no_conflicts', 'override'}, optional
String indicating how to compare variables of the same name for
Expand Down Expand Up @@ -581,6 +582,22 @@ def combine_by_coords(
temperature (x) float64 11.04 23.57 20.77 ...
"""

# Convert dict like objects to Dataset(s)
from .dataarray import DataArray
from .dataset import Dataset

dict_like_objects = list()
dcherian marked this conversation as resolved.
Show resolved Hide resolved
for obj in objects:
dcherian marked this conversation as resolved.
Show resolved Hide resolved
if not (isinstance(obj, (DataArray, Dataset))):
raise TypeError(
"objects must be an iterable containing only "
"Dataset(s) and/or DataArray(s)"
)

obj = obj.to_dataset() if isinstance(obj, DataArray) else obj
dict_like_objects.append(obj)
datasets = dict_like_objects

# Group by data vars
sorted_datasets = sorted(datasets, key=vars_as_keys)
grouped_by_vars = itertools.groupby(sorted_datasets, key=vars_as_keys)
Expand Down