Skip to content

Commit dcece9e

Browse files
committed
concat keep attrs from first variable.
Fixes #2060 Fixes #2575
1 parent 2ee89c3 commit dcece9e

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Breaking changes
2525

2626
New Features
2727
~~~~~~~~~~~~
28+
- :py:func:`xarray.concat` now preserves attributes from the first Variable.
29+
(:issue:`2575`, :issue:`2060`, :issue:`1614`)
30+
By `Deepak Cherian <https://github.com/dcherian>`_.
2831
- :py:meth:`Dataset.quantile`, :py:meth:`DataArray.quantile` and ``GroupBy.quantile``
2932
now work with dask Variables.
3033
By `Deepak Cherian <https://github.com/dcherian>`_.

xarray/core/concat.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ def concat(
9393
those of the first object with that dimension. Indexes for the same
9494
dimension must have the same size in all objects.
9595
96-
indexers, mode, concat_over : deprecated
97-
9896
Returns
9997
-------
10098
concatenated : type of objs
10199
100+
Notes
101+
-----
102+
Each concatenated Variable preserves corresponding ``attrs`` from the first element of ``objs``.
103+
102104
See also
103105
--------
104106
merge

xarray/core/variable.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1622,8 +1622,9 @@ def concat(cls, variables, dim="concat_dim", positions=None, shortcut=False):
16221622
if not shortcut:
16231623
for var in variables:
16241624
if var.dims != first_var.dims:
1625-
raise ValueError("inconsistent dimensions")
1626-
utils.remove_incompatible_items(attrs, var.attrs)
1625+
raise ValueError(
1626+
f"Variable has dimensions {list(var.dims)} but first Variable has dimensions {list(first_var.dims)}"
1627+
)
16271628

16281629
return cls(dims, data, attrs, encoding)
16291630

xarray/tests/test_concat.py

+13
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,16 @@ def test_concat_join_kwarg(self):
462462
for join in expected:
463463
actual = concat([ds1, ds2], join=join, dim="x")
464464
assert_equal(actual, expected[join].to_array())
465+
466+
467+
@pytest.mark.parametrize("attr1", ({"a": {"meta": [10, 20, 30]}}, {"a": [1, 2, 3]}, {}))
468+
@pytest.mark.parametrize("attr2", ({"a": [1, 2, 3]}, {}))
469+
def test_concat_attrs_first_variable(attr1, attr2):
470+
471+
arrs = [
472+
DataArray([[1], [2]], dims=["x", "y"], attrs=attr1),
473+
DataArray([[3], [4]], dims=["x", "y"], attrs=attr2),
474+
]
475+
476+
concat_attrs = concat(arrs, "y").attrs
477+
assert concat_attrs == attr1

0 commit comments

Comments
 (0)