Skip to content

Commit c33dab2

Browse files
TomNicholasshoyer
authored andcommitted
Improve name concat (#2792)
* Added tests of desired name inferring behaviour * Infers names * updated what's new
1 parent e8eb83b commit c33dab2

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

doc/whats-new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ Bug fixes
110110
- Fixed error when trying to reduce a DataArray using a function which does not
111111
require an axis argument. (:issue:`2768`)
112112
By `Tom Nicholas <http://github.com/TomNicholas>`_.
113+
- Concatenating a sequence of :py:class:`~xarray.DataArray` with varying names
114+
sets the name of the output array to ``None``, instead of the name of the
115+
first input array. If the names are the same it sets the name to that,
116+
instead to the name of the first DataArray in the list as it did before.
117+
(:issue:`2775`). By `Tom Nicholas <http://github.com/TomNicholas>`_.
113118

114119
- Per `CF conventions
115120
<http://cfconventions.org/cf-conventions/cf-conventions.html#calendar>`_,

xarray/core/combine.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .merge import merge
1010
from .variable import IndexVariable, Variable, as_variable
1111
from .variable import concat as concat_vars
12+
from .computation import result_name
1213

1314

1415
def concat(objs, dim=None, data_vars='all', coords='different',
@@ -336,7 +337,10 @@ def _dataarray_concat(arrays, dim, data_vars, coords, compat,
336337

337338
ds = _dataset_concat(datasets, dim, data_vars, coords, compat,
338339
positions)
339-
return arrays[0]._from_temp_dataset(ds, name)
340+
result = arrays[0]._from_temp_dataset(ds, name)
341+
342+
result.name = result_name(arrays)
343+
return result
340344

341345

342346
def _auto_concat(datasets, dim=None, data_vars='all', coords='different'):

xarray/tests/test_combine.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ def test_concat_encoding(self):
285285
assert concat([foo, foo], dim="x").encoding == foo.encoding
286286
assert concat([ds, ds], dim="x").encoding == ds.encoding
287287

288+
@pytest.mark.parametrize("colors, expected_name",
289+
[(['blue', 'green', 'red'], None),
290+
(['red', 'red', 'red'], 'red')])
291+
def test_concat_determine_name(self, colors, expected_name):
292+
das = [DataArray(np.random.random((2, 2)), dims=['x', 'y'], name=k)
293+
for k in colors]
294+
result = concat(das, dim="band")
295+
assert result.name is expected_name
296+
288297
@requires_dask
289298
def test_concat_lazy(self):
290299
import dask.array as da

0 commit comments

Comments
 (0)