Skip to content

Commit 9588afc

Browse files
TomNicholaspletchm
authored andcommitted
Improve name concat (pydata#2792)
* Added tests of desired name inferring behaviour * Infers names * updated what's new
1 parent 93185ef commit 9588afc

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/whats-new.rst

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

118123
- Per `CF conventions
119124
<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)