Skip to content

Improve name concat #2792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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 <http://github.com/TomNicholas>`_.
- 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 <http://github.com/TomNicholas>`_.

- Per `CF conventions
<http://cfconventions.org/cf-conventions/cf-conventions.html#calendar>`_,
Expand Down
6 changes: 5 additions & 1 deletion xarray/core/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'):
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down