Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmonize returned multi-indexed indexes when applying concat along new dimension #6889

Merged
merged 11 commits into from
Aug 25, 2022
Merged

Harmonize returned multi-indexed indexes when applying concat along new dimension #6889

merged 11 commits into from
Aug 25, 2022

Conversation

FabianHofmann
Copy link
Contributor

In the current implementation, the concat function does not ensure that the indexes that belong to the same MultiIndex relate to the same MultiIndex object in Dataset.indexes when concatenating along a new dimension. This becomes a problem as soon as the returned dataset needs to be aligned (for broadcasting, reindexing etc.), see #6881 for example. As far as I understand, this bug was introduced in #5692 following the idea that the concat function should disentangle indexes and dimensions.

It can be fixed by not removing the index name from the list of indexes which should be merged, see

variables_to_merge = (coord_names | data_names) - concat_over - dim_names
. All indexes contained in this list will get a new index object. Currently, this list only contains the levels of a multi-indexed index, not the index name itself. This is removed as it is contained in dim_names. Instead of removing all dimension names from this list, I suggest to only remove unlabeled dimensions from this list.

Copy link
Member

@benbovy benbovy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @FabianHofmann for the #6881 issue report and for the fix, that makes a lot of sense! It will also fix the general case of any custom (non-pandas) multi-coordinate index with one of the names matching a dimension name.

I left a minor suggestion below.

xarray/tests/test_concat.py Outdated Show resolved Hide resolved
@FabianHofmann
Copy link
Contributor Author

Thanks @benbovy¸ glad that the proposed fix can help here!

@FabianHofmann FabianHofmann requested a review from benbovy August 15, 2022 10:27
@FabianHofmann
Copy link
Contributor Author

@benbovy I applied your suggestion (+ a small fix) and merged with the up to date main branch. All tests pass.

@benbovy
Copy link
Member

benbovy commented Aug 23, 2022

LGTM, thanks @FabianHofmann! Could you update whats-new.rst in the docs? I think this bug fix is worth to be mentioned there with your contribution.

@FabianHofmann
Copy link
Contributor Author

@benbovy done :)

doc/whats-new.rst Outdated Show resolved Hide resolved
Co-authored-by: Benoit Bovy <[email protected]>
@benbovy
Copy link
Member

benbovy commented Aug 25, 2022

All good, thanks @FabianHofmann !

@benbovy benbovy merged commit 15c6182 into pydata:main Aug 25, 2022
@FabianHofmann
Copy link
Contributor Author

Great, thanks for the nice review @benbovy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Alignment of dataset with MultiIndex fails after applying xr.concat
2 participants