Skip to content

Commit 9a7ab2b

Browse files
authored
Error concat dataset and dataarray (#5425)
* add type checking for datasets concatenation * add type checking for dataarrays concatenation * add concatenation type checking test * edit type checking for datasets concatenation * fixing W293 concat.py * edit error message * edit error message * fix typos * fix typos * fix linting * add trailing commas * Update whats-new.rst * fix linting * fix linting
1 parent ddb058b commit 9a7ab2b

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ New Features
2929
values if inputs are dask arrays (:issue:`4804`, :pull:`5284`).
3030
By `Andrew Williams <https://github.com/AndrewWilliams3142>`_.
3131

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>`_.
3234

3335
Breaking changes
3436
~~~~~~~~~~~~~~~~

xarray/core/concat.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ def _dataset_concat(
422422
"""
423423
from .dataset import Dataset
424424

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+
425432
dim, coord = _calc_concat_dim_coord(dim)
426433
# Make sure we're working on a copy (we'll be loading variables)
427434
datasets = [ds.copy() for ds in datasets]
@@ -541,8 +548,15 @@ def _dataarray_concat(
541548
join: str = "outer",
542549
combine_attrs: str = "override",
543550
) -> "DataArray":
551+
from .dataarray import DataArray
552+
544553
arrays = list(arrays)
545554

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+
546560
if data_vars != "all":
547561
raise ValueError(
548562
"data_vars is not a valid argument when concatenating DataArray objects"

xarray/tests/test_concat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,20 @@ def test_concat_preserve_coordinate_order():
730730
for act, exp in zip(actual.coords, expected.coords):
731731
assert act == exp
732732
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")

0 commit comments

Comments
 (0)