Skip to content

Commit 7d389db

Browse files
kmuehlbauerdcheriankeewis
authored
preserve original dimension, coordinate and variable order in concat (#4419)
* preserve original dimension, coordinate and variable order in ``concat`` * only re-insert into result_vars if already in * add test to check if dimension and coordinate order is preserved in concat * black style * Update xarray/tests/test_concat.py Co-authored-by: keewis <[email protected]> * Update xarray/tests/test_concat.py * add whats-new.rst entry * fix scalar variable problem in test_concat Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: keewis <[email protected]>
1 parent 2ed6d57 commit 7d389db

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Bug fixes
8686
By `Jens Svensmark <https://github.com/jenssss>`_
8787
- Fix incorrect legend labels for :py:meth:`Dataset.plot.scatter` (:issue:`4126`).
8888
By `Peter Hausamann <https://github.com/phausamann>`_.
89+
- Preserve dimension and coordinate order during :py:func:`xarray.concat` (:issue:`2811`, :issue:`4072`, :pull:`4419`).
90+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
8991
- Avoid relying on :py:class:`set` objects for the ordering of the coordinates (:pull:`4409`)
9092
By `Justus Magin <https://github.com/keewis>`_.
9193
- Fix indexing with datetime64 scalars with pandas 1.1 (:issue:`4283`).

xarray/core/concat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ def ensure_common_dims(vars):
463463
combined = concat_vars(vars, dim, positions)
464464
assert isinstance(combined, Variable)
465465
result_vars[k] = combined
466+
elif k in result_vars:
467+
# preserves original variable order
468+
result_vars[k] = result_vars.pop(k)
466469

467470
result = Dataset(result_vars, attrs=result_attrs)
468471
absent_coord_names = coord_names - set(result.variables)

xarray/tests/test_concat.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,36 @@ def test_concat_merge_single_non_dim_coord():
558558
for coords in ["different", "all"]:
559559
with raises_regex(ValueError, "'y' not present in all datasets"):
560560
concat([da1, da2, da3], dim="x")
561+
562+
563+
def test_concat_preserve_coordinate_order():
564+
x = np.arange(0, 5)
565+
y = np.arange(0, 10)
566+
time = np.arange(0, 4)
567+
data = np.zeros((4, 10, 5), dtype=bool)
568+
569+
ds1 = Dataset(
570+
{"data": (["time", "y", "x"], data[0:2])},
571+
coords={"time": time[0:2], "y": y, "x": x},
572+
)
573+
ds2 = Dataset(
574+
{"data": (["time", "y", "x"], data[2:4])},
575+
coords={"time": time[2:4], "y": y, "x": x},
576+
)
577+
578+
expected = Dataset(
579+
{"data": (["time", "y", "x"], data)},
580+
coords={"time": time, "y": y, "x": x},
581+
)
582+
583+
actual = concat([ds1, ds2], dim="time")
584+
585+
# check dimension order
586+
for act, exp in zip(actual.dims, expected.dims):
587+
assert act == exp
588+
assert actual.dims[act] == expected.dims[exp]
589+
590+
# check coordinate order
591+
for act, exp in zip(actual.coords, expected.coords):
592+
assert act == exp
593+
assert_identical(actual.coords[act], expected.coords[exp])

0 commit comments

Comments
 (0)