Skip to content

Commit 15c6182

Browse files
Fabian Hofmannpre-commit-ci[bot]benbovy
authored
Harmonize returned multi-indexed indexes when applying concat along new dimension (#6889)
* test: add unit test for #6881 * fix: harmonize multiindex objects when concat'ing along new dim * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update xarray/tests/test_concat.py Co-authored-by: Benoit Bovy <[email protected]> * fix: use directly imported concat function in test_concat * test: fix level names in concat test function * doc: add note to whats-new.rst * Update doc/whats-new.rst Co-authored-by: Benoit Bovy <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Benoit Bovy <[email protected]>
1 parent 790a444 commit 15c6182

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Bug fixes
5151
By `Oliver Lopez <https://github.com/lopezvoliver>`_.
5252
- Fix bug where index variables would be changed inplace (:issue:`6931`, :pull:`6938`)
5353
By `Michael Niklas <https://github.com/headtr1ck>`_.
54+
- Harmonize returned multi-indexed indexes when applying ``concat`` along new dimension (:issue:`6881`, :pull:`6889`)
55+
By `Fabian Hofmann <https://github.com/FabianHofmann>`_.
5456

5557
Documentation
5658
~~~~~~~~~~~~~

xarray/core/concat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def _dataset_concat(
490490
)
491491

492492
# determine which variables to merge, and then merge them according to compat
493-
variables_to_merge = (coord_names | data_names) - concat_over - dim_names
493+
variables_to_merge = (coord_names | data_names) - concat_over - unlabeled_dims
494494

495495
result_vars = {}
496496
result_indexes = {}

xarray/tests/test_concat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ def test_concat_multiindex(self) -> None:
513513
assert expected.equals(actual)
514514
assert isinstance(actual.x.to_index(), pd.MultiIndex)
515515

516+
def test_concat_along_new_dim_multiindex(self) -> None:
517+
# see https://github.com/pydata/xarray/issues/6881
518+
level_names = ["x_level_0", "x_level_1"]
519+
x = pd.MultiIndex.from_product([[1, 2, 3], ["a", "b"]], names=level_names)
520+
ds = Dataset(coords={"x": x})
521+
concatenated = concat([ds], "new")
522+
actual = list(concatenated.xindexes.get_all_coords("x"))
523+
expected = ["x"] + level_names
524+
assert actual == expected
525+
516526
@pytest.mark.parametrize("fill_value", [dtypes.NA, 2, 2.0, {"a": 2, "b": 1}])
517527
def test_concat_fill_value(self, fill_value) -> None:
518528
datasets = [

0 commit comments

Comments
 (0)