File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,8 @@ New Features
29
29
values if inputs are dask arrays (:issue: `4804 `, :pull: `5284 `).
30
30
By `Andrew Williams <https://github.com/AndrewWilliams3142 >`_.
31
31
32
+ - Attempting to ``concat `` list of elements that are not all ``Dataset `` or all ``DataArray `` now raises an error (:issue: `5051 `, :pull: `5425 `).
33
+ By `Thomas Hirtz <https://github.com/thomashirtz >`_.
32
34
33
35
Breaking changes
34
36
~~~~~~~~~~~~~~~~
Original file line number Diff line number Diff line change @@ -422,6 +422,13 @@ def _dataset_concat(
422
422
"""
423
423
from .dataset import Dataset
424
424
425
+ datasets = list (datasets )
426
+
427
+ if not all (isinstance (dataset , Dataset ) for dataset in datasets ):
428
+ raise TypeError (
429
+ "The elements in the input list need to be either all 'Dataset's or all 'DataArray's"
430
+ )
431
+
425
432
dim , coord = _calc_concat_dim_coord (dim )
426
433
# Make sure we're working on a copy (we'll be loading variables)
427
434
datasets = [ds .copy () for ds in datasets ]
@@ -541,8 +548,15 @@ def _dataarray_concat(
541
548
join : str = "outer" ,
542
549
combine_attrs : str = "override" ,
543
550
) -> "DataArray" :
551
+ from .dataarray import DataArray
552
+
544
553
arrays = list (arrays )
545
554
555
+ if not all (isinstance (array , DataArray ) for array in arrays ):
556
+ raise TypeError (
557
+ "The elements in the input list need to be either all 'Dataset's or all 'DataArray's"
558
+ )
559
+
546
560
if data_vars != "all" :
547
561
raise ValueError (
548
562
"data_vars is not a valid argument when concatenating DataArray objects"
Original file line number Diff line number Diff line change @@ -730,3 +730,20 @@ def test_concat_preserve_coordinate_order():
730
730
for act , exp in zip (actual .coords , expected .coords ):
731
731
assert act == exp
732
732
assert_identical (actual .coords [act ], expected .coords [exp ])
733
+
734
+
735
+ def test_concat_typing_check ():
736
+ ds = Dataset ({"foo" : 1 }, {"bar" : 2 })
737
+ da = Dataset ({"foo" : 3 }, {"bar" : 4 }).to_array (dim = "foo" )
738
+
739
+ # concatenate a list of non-homogeneous types must raise TypeError
740
+ with pytest .raises (
741
+ TypeError ,
742
+ match = "The elements in the input list need to be either all 'Dataset's or all 'DataArray's" ,
743
+ ):
744
+ concat ([ds , da ], dim = "foo" )
745
+ with pytest .raises (
746
+ TypeError ,
747
+ match = "The elements in the input list need to be either all 'Dataset's or all 'DataArray's" ,
748
+ ):
749
+ concat ([da , ds ], dim = "foo" )
You can’t perform that action at this time.
0 commit comments