From c33dab237a0aca316b7f6d4ba10857829a895a25 Mon Sep 17 00:00:00 2001 From: Tom Nicholas <35968931+TomNicholas@users.noreply.github.com> Date: Mon, 4 Mar 2019 05:39:20 +0000 Subject: [PATCH] Improve name concat (#2792) * Added tests of desired name inferring behaviour * Infers names * updated what's new --- doc/whats-new.rst | 5 +++++ xarray/core/combine.py | 6 +++++- xarray/tests/test_combine.py | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index aad6bb41873..4218771848c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -110,6 +110,11 @@ Bug fixes - Fixed error when trying to reduce a DataArray using a function which does not require an axis argument. (:issue:`2768`) By `Tom Nicholas `_. +- Concatenating a sequence of :py:class:`~xarray.DataArray` with varying names + sets the name of the output array to ``None``, instead of the name of the + first input array. If the names are the same it sets the name to that, + instead to the name of the first DataArray in the list as it did before. + (:issue:`2775`). By `Tom Nicholas `_. - Per `CF conventions `_, diff --git a/xarray/core/combine.py b/xarray/core/combine.py index 11961dff520..1abd14cd20b 100644 --- a/xarray/core/combine.py +++ b/xarray/core/combine.py @@ -9,6 +9,7 @@ from .merge import merge from .variable import IndexVariable, Variable, as_variable from .variable import concat as concat_vars +from .computation import result_name def concat(objs, dim=None, data_vars='all', coords='different', @@ -336,7 +337,10 @@ def _dataarray_concat(arrays, dim, data_vars, coords, compat, ds = _dataset_concat(datasets, dim, data_vars, coords, compat, positions) - return arrays[0]._from_temp_dataset(ds, name) + result = arrays[0]._from_temp_dataset(ds, name) + + result.name = result_name(arrays) + return result def _auto_concat(datasets, dim=None, data_vars='all', coords='different'): diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index c37abc98f07..0d03b6e0cdf 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -285,6 +285,15 @@ def test_concat_encoding(self): assert concat([foo, foo], dim="x").encoding == foo.encoding assert concat([ds, ds], dim="x").encoding == ds.encoding + @pytest.mark.parametrize("colors, expected_name", + [(['blue', 'green', 'red'], None), + (['red', 'red', 'red'], 'red')]) + def test_concat_determine_name(self, colors, expected_name): + das = [DataArray(np.random.random((2, 2)), dims=['x', 'y'], name=k) + for k in colors] + result = concat(das, dim="band") + assert result.name is expected_name + @requires_dask def test_concat_lazy(self): import dask.array as da