Skip to content

Commit

Permalink
Improve name concat (#2792)
Browse files Browse the repository at this point in the history
* Added tests of desired name inferring behaviour

* Infers names

* updated what's new
  • Loading branch information
TomNicholas authored and shoyer committed Mar 4, 2019
1 parent e8eb83b commit c33dab2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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

0 comments on commit c33dab2

Please sign in to comment.