Skip to content

Commit 01ba1f8

Browse files
committed
Better names for concat arrays
1 parent cd8e370 commit 01ba1f8

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ Bug fixes
7474

7575
- Silenced warnings that appear when using pandas 0.24.
7676
By `Stephan Hoyer <https://github.com/shoyer>`_
77+
- Concatenating a sequence of :py:class:`~xarray.DataArray` with varying names
78+
sets the name of the output array to ``None``, instead of the name of the
79+
first input array.
80+
(:issue:`2775`). By `Zac Hatfield-Dodds <https://github.com/Zac-HD>`_.
7781
- Interpolating via resample now internally specifies ``bounds_error=False``
7882
as an argument to ``scipy.interpolate.interp1d``, allowing for interpolation
7983
from higher frequencies to lower frequencies. Datapoints outside the bounds

xarray/core/combine.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from . import utils
88
from .alignment import align
9+
from .computation import result_name
910
from .merge import merge
1011
from .variable import IndexVariable, Variable, as_variable
1112
from .variable import concat as concat_vars
@@ -323,16 +324,10 @@ def _dataarray_concat(arrays, dim, data_vars, coords, compat,
323324
raise ValueError('data_vars is not a valid argument when '
324325
'concatenating DataArray objects')
325326

326-
datasets = []
327-
for n, arr in enumerate(arrays):
328-
if n == 0:
329-
name = arr.name
330-
elif name != arr.name:
331-
if compat == 'identical':
332-
raise ValueError('array names not identical')
333-
else:
334-
arr = arr.rename(name)
335-
datasets.append(arr._to_temp_dataset())
327+
name = result_name(arrays)
328+
if name is None and compat == 'identical':
329+
raise ValueError('array names not identical')
330+
datasets = [arr.rename(name)._to_temp_dataset() for arr in arrays]
336331

337332
ds = _dataset_concat(datasets, dim, data_vars, coords, compat,
338333
positions)

xarray/tests/test_combine.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,16 @@ def test_concat_lazy(self):
297297
assert combined.shape == (2, 3, 3)
298298
assert combined.dims == ('z', 'x', 'y')
299299

300+
def test_concat_names(self):
301+
ds = Dataset({'foo': (['x', 'y'], np.random.random((2, 2))),
302+
'bar': (['x', 'y'], np.random.random((2, 2)))})
303+
# Concat arrays with different names, new name is None
304+
new = concat([ds.foo, ds.bar], dim='new')
305+
assert new.name is None
306+
# Concat arrays with same name, name is preserved
307+
foobar = ds.foo.rename('bar')
308+
assert concat([foobar, ds.bar], dim='new').name == 'bar'
309+
300310

301311
class TestAutoCombine(object):
302312

0 commit comments

Comments
 (0)