@@ -356,6 +356,14 @@ def _nested_combine(
356
356
# Check that the inferred shape is combinable
357
357
_check_shape_tile_ids (combined_ids )
358
358
359
+ # Promote any DataArrays to Datasets
360
+ for id , obj in combined_ids .items ():
361
+ if isinstance (obj , DataArray ):
362
+ if obj .name is None :
363
+ combined_ids [id ] = obj ._to_temp_dataset ()
364
+ else :
365
+ combined_ids [id ] = obj .to_dataset ()
366
+
359
367
# Apply series of concatenate or merge operations along each dimension
360
368
combined = _combine_nd (
361
369
combined_ids ,
@@ -570,31 +578,59 @@ def combine_nested(
570
578
merge
571
579
combine_by_coords
572
580
"""
573
-
574
- mixed_datasets_and_arrays = any (
575
- isinstance (obj , Dataset ) for obj in iterate_nested (datasets )
576
- ) and any (
577
- isinstance (obj , DataArray ) and obj .name is None
578
- for obj in iterate_nested (datasets )
579
- )
580
- if mixed_datasets_and_arrays :
581
- raise ValueError ("Can't combine datasets with unnamed arrays." )
581
+
582
+ # TODO deprecation cycle to change the name of this argument...
583
+ data_objects = datasets
582
584
583
585
if isinstance (concat_dim , (str , DataArray )) or concat_dim is None :
584
586
concat_dim = [concat_dim ]
585
587
586
- # The IDs argument tells _nested_combine that datasets aren't yet sorted
587
- return _nested_combine (
588
- datasets ,
589
- concat_dims = concat_dim ,
590
- compat = compat ,
591
- data_vars = data_vars ,
592
- coords = coords ,
593
- ids = False ,
594
- fill_value = fill_value ,
595
- join = join ,
596
- combine_attrs = combine_attrs ,
597
- )
588
+ objs_are_unnamed_dataarrays = [
589
+ isinstance (data_object , DataArray ) and data_object .name is None
590
+ for data_object in iterate_nested (data_objects )
591
+ ]
592
+ if any (objs_are_unnamed_dataarrays ):
593
+ if all (objs_are_unnamed_dataarrays ):
594
+ # Combine into a single larger DataArray
595
+ unnamed_arrays = data_objects
596
+
597
+ combined_temp_dataset = _nested_combine (
598
+ unnamed_arrays ,
599
+ concat_dims = concat_dim ,
600
+ compat = compat ,
601
+ data_vars = data_vars ,
602
+ coords = coords ,
603
+ ids = False ,
604
+ fill_value = fill_value ,
605
+ join = join ,
606
+ combine_attrs = combine_attrs ,
607
+ )
608
+ return DataArray ()._from_temp_dataset (combined_temp_dataset )
609
+ else :
610
+ # Must be a mix of unnamed dataarrays with either named dataarrays or with datasets
611
+ # Can't combine these as we wouldn't know whether to merge or concatenate the arrays
612
+ raise ValueError (
613
+ "Can't automatically combine unnamed dataarrays with either named dataarrays or datasets."
614
+ )
615
+ else :
616
+ # Promote any named DataArrays to single-variable Datasets to simplify combining
617
+ # data_objects = [
618
+ # obj.to_dataset() if isinstance(obj, DataArray) else obj
619
+ # for obj in data_objects
620
+ # ]
621
+
622
+ # The IDs argument tells _nested_combine that datasets aren't yet sorted
623
+ return _nested_combine (
624
+ data_objects ,
625
+ concat_dims = concat_dim ,
626
+ compat = compat ,
627
+ data_vars = data_vars ,
628
+ coords = coords ,
629
+ ids = False ,
630
+ fill_value = fill_value ,
631
+ join = join ,
632
+ combine_attrs = combine_attrs ,
633
+ )
598
634
599
635
600
636
def vars_as_keys (ds ):
@@ -700,7 +736,6 @@ def combine_by_coords(
700
736
----------
701
737
data_objects : sequence of xarray.Dataset or sequence of xarray.DataArray
702
738
Data objects to combine.
703
-
704
739
compat : {"identical", "equals", "broadcast_equals", "no_conflicts", "override"}, optional
705
740
String indicating how to compare variables of the same name for
706
741
potential conflicts:
0 commit comments