Skip to content

Commit

Permalink
Harmonize returned multi-indexed indexes when applying concat along…
Browse files Browse the repository at this point in the history
… 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]>
  • Loading branch information
3 people authored Aug 25, 2022
1 parent 790a444 commit 15c6182
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Bug fixes
By `Oliver Lopez <https://github.com/lopezvoliver>`_.
- Fix bug where index variables would be changed inplace (:issue:`6931`, :pull:`6938`)
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Harmonize returned multi-indexed indexes when applying ``concat`` along new dimension (:issue:`6881`, :pull:`6889`)
By `Fabian Hofmann <https://github.com/FabianHofmann>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def _dataset_concat(
)

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

result_vars = {}
result_indexes = {}
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,16 @@ def test_concat_multiindex(self) -> None:
assert expected.equals(actual)
assert isinstance(actual.x.to_index(), pd.MultiIndex)

def test_concat_along_new_dim_multiindex(self) -> None:
# see https://github.com/pydata/xarray/issues/6881
level_names = ["x_level_0", "x_level_1"]
x = pd.MultiIndex.from_product([[1, 2, 3], ["a", "b"]], names=level_names)
ds = Dataset(coords={"x": x})
concatenated = concat([ds], "new")
actual = list(concatenated.xindexes.get_all_coords("x"))
expected = ["x"] + level_names
assert actual == expected

@pytest.mark.parametrize("fill_value", [dtypes.NA, 2, 2.0, {"a": 2, "b": 1}])
def test_concat_fill_value(self, fill_value) -> None:
datasets = [
Expand Down

0 comments on commit 15c6182

Please sign in to comment.