12
12
combine_by_coords ,
13
13
combine_nested ,
14
14
concat ,
15
+ merge ,
15
16
)
16
17
from xarray .core import dtypes
17
18
from xarray .core .combine import (
@@ -688,7 +689,7 @@ def test_nested_combine_mixed_datasets_arrays(self):
688
689
combine_nested (objs , "x" )
689
690
690
691
691
- class TestCombineAuto :
692
+ class TestCombineDatasetsbyCoords :
692
693
def test_combine_by_coords (self ):
693
694
objs = [Dataset ({"x" : [0 ]}), Dataset ({"x" : [1 ]})]
694
695
actual = combine_by_coords (objs )
@@ -730,17 +731,6 @@ def test_combine_by_coords(self):
730
731
def test_empty_input (self ):
731
732
assert_identical (Dataset (), combine_by_coords ([]))
732
733
733
- def test_combine_coords_mixed_datasets_arrays (self ):
734
- objs = [
735
- DataArray ([0 , 1 ], dims = ("x" ), coords = ({"x" : [0 , 1 ]})),
736
- Dataset ({"x" : [2 , 3 ]}),
737
- ]
738
- with pytest .raises (
739
- ValueError ,
740
- match = r"Can't automatically combine datasets with unnamed arrays." ,
741
- ):
742
- combine_by_coords (objs )
743
-
744
734
@pytest .mark .parametrize (
745
735
"join, expected" ,
746
736
[
@@ -1044,7 +1034,35 @@ def test_combine_by_coords_incomplete_hypercube(self):
1044
1034
with pytest .raises (ValueError ):
1045
1035
combine_by_coords ([x1 , x2 , x3 ], fill_value = None )
1046
1036
1047
- def test_combine_by_coords_unnamed_arrays (self ):
1037
+
1038
+ class TestCombineMixedObjectsbyCoords :
1039
+ def test_combine_by_coords_mixed_unnamed_dataarrays (self ):
1040
+ named_da = DataArray (name = "a" , data = [1.0 , 2.0 ], coords = {"x" : [0 , 1 ]}, dims = "x" )
1041
+ unnamed_da = DataArray (data = [3.0 , 4.0 ], coords = {"x" : [2 , 3 ]}, dims = "x" )
1042
+
1043
+ with pytest .raises (
1044
+ ValueError , match = "Can't automatically combine unnamed DataArrays with"
1045
+ ):
1046
+ combine_by_coords ([named_da , unnamed_da ])
1047
+
1048
+ da = DataArray ([0 , 1 ], dims = "x" , coords = ({"x" : [0 , 1 ]}))
1049
+ ds = Dataset ({"x" : [2 , 3 ]})
1050
+ with pytest .raises (
1051
+ ValueError ,
1052
+ match = "Can't automatically combine unnamed DataArrays with" ,
1053
+ ):
1054
+ combine_by_coords ([da , ds ])
1055
+
1056
+ def test_combine_coords_mixed_datasets_named_dataarrays (self ):
1057
+ da = DataArray (name = "a" , data = [4 , 5 ], dims = "x" , coords = ({"x" : [0 , 1 ]}))
1058
+ ds = Dataset ({"b" : ("x" , [2 , 3 ])})
1059
+ actual = combine_by_coords ([da , ds ])
1060
+ expected = Dataset (
1061
+ {"a" : ("x" , [4 , 5 ]), "b" : ("x" , [2 , 3 ])}, coords = {"x" : ("x" , [0 , 1 ])}
1062
+ )
1063
+ assert_identical (expected , actual )
1064
+
1065
+ def test_combine_by_coords_all_unnamed_dataarrays (self ):
1048
1066
unnamed_array = DataArray (data = [1.0 , 2.0 ], coords = {"x" : [0 , 1 ]}, dims = "x" )
1049
1067
1050
1068
actual = combine_by_coords ([unnamed_array ])
@@ -1060,6 +1078,33 @@ def test_combine_by_coords_unnamed_arrays(self):
1060
1078
)
1061
1079
assert_identical (expected , actual )
1062
1080
1081
+ def test_combine_by_coords_all_named_dataarrays (self ):
1082
+ named_da = DataArray (name = "a" , data = [1.0 , 2.0 ], coords = {"x" : [0 , 1 ]}, dims = "x" )
1083
+
1084
+ actual = combine_by_coords ([named_da ])
1085
+ expected = named_da .to_dataset ()
1086
+ assert_identical (expected , actual )
1087
+
1088
+ named_da1 = DataArray (name = "a" , data = [1.0 , 2.0 ], coords = {"x" : [0 , 1 ]}, dims = "x" )
1089
+ named_da2 = DataArray (name = "b" , data = [3.0 , 4.0 ], coords = {"x" : [2 , 3 ]}, dims = "x" )
1090
+
1091
+ actual = combine_by_coords ([named_da1 , named_da2 ])
1092
+ expected = Dataset (
1093
+ {
1094
+ "a" : DataArray (data = [1.0 , 2.0 ], coords = {"x" : [0 , 1 ]}, dims = "x" ),
1095
+ "b" : DataArray (data = [3.0 , 4.0 ], coords = {"x" : [2 , 3 ]}, dims = "x" ),
1096
+ }
1097
+ )
1098
+ assert_identical (expected , actual )
1099
+
1100
+ def test_combine_by_coords_all_dataarrays_with_the_same_name (self ):
1101
+ named_da1 = DataArray (name = "a" , data = [1.0 , 2.0 ], coords = {"x" : [0 , 1 ]}, dims = "x" )
1102
+ named_da2 = DataArray (name = "a" , data = [3.0 , 4.0 ], coords = {"x" : [2 , 3 ]}, dims = "x" )
1103
+
1104
+ actual = combine_by_coords ([named_da1 , named_da2 ])
1105
+ expected = merge ([named_da1 , named_da2 ])
1106
+ assert_identical (expected , actual )
1107
+
1063
1108
1064
1109
@requires_cftime
1065
1110
def test_combine_by_coords_distant_cftime_dates ():
0 commit comments