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

Alignment of dataset with MultiIndex fails after applying xr.concat #6881

Closed
4 tasks done
FabianHofmann opened this issue Aug 5, 2022 · 0 comments · Fixed by #6889
Closed
4 tasks done

Alignment of dataset with MultiIndex fails after applying xr.concat #6881

FabianHofmann opened this issue Aug 5, 2022 · 0 comments · Fixed by #6889

Comments

@FabianHofmann
Copy link
Contributor

What happened?

After applying the concat function to a dataset with a Multiindex, a lot of functions related to indexing are broken. For example, it is not possible to apply reindex_like to itself anymore.

The error is raised in the alignment module. It seems that the function find_matching_indexes does not find indexes that belong to the same dimension.

What did you expect to happen?

I expected the alignment to be functional and that these basic functions work.

Minimal Complete Verifiable Example

import xarray as xr
import pandas as pd

index = pd.MultiIndex.from_product([[1,2], ['a', 'b']], names=('level1', 'level2'))
index.name = 'dim'

var = xr.DataArray(1, coords=[index])
ds = xr.Dataset({"var":var})

new = xr.concat([ds], dim='newdim')
xr.Dataset(new) # breaks
new.reindex_like(new) # breaks

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

Traceback (most recent call last):

  File "/tmp/ipykernel_407170/4030736219.py", line 11, in <cell line: 11>
    xr.Dataset(new) # breaks

  File "/home/fabian/.miniconda3/lib/python3.10/site-packages/xarray/core/dataset.py", line 599, in __init__
    variables, coord_names, dims, indexes, _ = merge_data_and_coords(

  File "/home/fabian/.miniconda3/lib/python3.10/site-packages/xarray/core/merge.py", line 575, in merge_data_and_coords
    return merge_core(

  File "/home/fabian/.miniconda3/lib/python3.10/site-packages/xarray/core/merge.py", line 752, in merge_core
    aligned = deep_align(

  File "/home/fabian/.miniconda3/lib/python3.10/site-packages/xarray/core/alignment.py", line 827, in deep_align
    aligned = align(

  File "/home/fabian/.miniconda3/lib/python3.10/site-packages/xarray/core/alignment.py", line 764, in align
    aligner.align()

  File "/home/fabian/.miniconda3/lib/python3.10/site-packages/xarray/core/alignment.py", line 550, in align
    self.assert_no_index_conflict()

  File "/home/fabian/.miniconda3/lib/python3.10/site-packages/xarray/core/alignment.py", line 319, in assert_no_index_conflict
    raise ValueError(

ValueError: cannot re-index or align objects with conflicting indexes found for the following dimensions: 'dim' (2 conflicting indexes)
Conflicting indexes may occur when
- they relate to different sets of coordinate and/or dimension names
- they don't have the same type
- they may be used to reindex data along common dimensions

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:04:59) [GCC 10.3.0]
python-bits: 64
OS: Linux
OS-release: 5.15.0-41-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1

xarray: 2022.6.0
pandas: 1.4.2
numpy: 1.21.6
scipy: 1.8.1
netCDF4: 1.6.0
pydap: None
h5netcdf: None
h5py: 3.6.0
Nio: None
zarr: None
cftime: 1.5.1.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.2.10
cfgrib: None
iris: None
bottleneck: 1.3.4
dask: 2022.6.1
distributed: 2022.6.1
matplotlib: 3.5.1
cartopy: 0.20.2
seaborn: 0.11.2
numbagg: None
fsspec: 2022.3.0
cupy: None
pint: None
sparse: 0.13.0
flox: None
numpy_groupies: None
setuptools: 61.2.0
pip: 22.1.2
conda: 4.13.0
pytest: 7.1.2
IPython: 7.33.0
sphinx: 5.0.2
/home/fabian/.miniconda3/lib/python3.10/site-packages/_distutils_hack/init.py:30: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

@FabianHofmann FabianHofmann added bug needs triage Issue that has not been reviewed by xarray team member labels Aug 5, 2022
@dcherian dcherian added regression and removed needs triage Issue that has not been reviewed by xarray team member labels Aug 5, 2022
benbovy added a commit that referenced this issue Aug 25, 2022
… 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants