pydata / pydata/xarray

ValueError: Resulting object does not have monotonic global indexes along dimension in combine_by_coords call

Open
#6,355 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-combine
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

When I try to do a combine_by_coords on 2 datasets x, y like

x1 = xr.Dataset(
    {

        "temperature": (("y", "x"), 20 * np.random.rand(6).reshape(2, 3)),
        "precipitation": (("y", "x"), np.random.rand(6).reshape(2, 3)),
    },
    coords={"y": [0, 1], "x": [20, 30, 40]},
)
x2 = xr.Dataset(
    {
        "temperature": (("y", "x"), 20 * np.random.rand(6).reshape(2, 3)),
        "precipitation": (("y", "x"), np.random.rand(6).reshape(2, 3)),
    },
    coords={"y": [2, 3], "x": [10, 20, 30]},
)

x1 looks like:

1|     x   x   x
0|     x   x   x  
  ----------------
       20  30  40

x2 Looks like:

3| y   y   y
2| y   y   y
  --------------
   10  20  30

I have this issue after a

ds_test  = xr.combine_by_coords([x2, x1])
ValueError: Resulting object does not have monotonic global indexes along dimension x 
What did you expect to happen?

For me the result of ds_test should be like:

3| y   y   y
2| y   y   y
1|     x   x   x
0|     x   x   x  
  ----------------
   10  20  30  40
Minimal Complete Verifiable Example
import numpy as np
import xarray as xr

print(xr.__version__)

x1 = xr.Dataset(
    {

        "temperature": (("y", "x"), 20 * np.random.rand(6).reshape(2, 3)),
        "precipitation": (("y", "x"), np.random.rand(6).reshape(2, 3)),
    },
    coords={"y": [0, 1], "x": [20, 30, 40]},
)
x2 = xr.Dataset(
    {
        "temperature": (("y", "x"), 20 * np.random.rand(6).reshape(2, 3)),
        "precipitation": (("y", "x"), np.random.rand(6).reshape(2, 3)),
    },
    coords={"y": [2, 3], "x": [10, 20, 30]},
)
print(x1)
print(x2)

ds_test  = xr.combine_by_coords([x2, x1])
Relevant log output
ValueError                                Traceback (most recent call last)
/home/thomas/Work/internal-applications/sungeodata/app-sungeodata/test copy.ipynb Cell 6' in <cell line: 1>()
----> 1 ds_test  = xr.combine_by_coords([x2, x1])

File ~/Work/internal-applications/sungeodata/app-sungeodata/.venv/lib/python3.8/site-packages/xarray/core/combine.py:979, in combine_by_coords(data_objects, compat, data_vars, coords, fill_value, join, combine_attrs, datasets)
    977     concatenated_grouped_by_data_vars = []
    978     for vars, datasets_with_same_vars in grouped_by_vars:
--> 979         concatenated = _combine_single_variable_hypercube(
    980             list(datasets_with_same_vars),
    981             fill_value=fill_value,
    982             data_vars=data_vars,
    983             coords=coords,
    984             compat=compat,
    985             join=join,
    986             combine_attrs=combine_attrs,
    987         )
    988         concatenated_grouped_by_data_vars.append(concatenated)
    990 return merge(
    991     concatenated_grouped_by_data_vars,
    992     compat=compat,
   (...)
    995     combine_attrs=combine_attrs,
    996 )

File ~/Work/internal-applications/sungeodata/app-sungeodata/.venv/lib/python3.8/site-packages/xarray/core/combine.py:653, in _combine_single_variable_hypercube(datasets, fill_value, data_vars, coords, compat, join, combine_attrs)
    651     indexes = concatenated.indexes.get(dim)
    652     if not (indexes.is_monotonic_increasing or indexes.is_monotonic_decreasing):
--> 653         raise ValueError(
    654             f"Resulting object does not have monotonic"
    655             f" global indexes along dimension {dim}"
    659         )
    661 return concatenated

ValueError: Resulting object does not have monotonic global indexes along dimension x 


I add more logs with:

```python
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/thomas/Work/internal-applications/sungeodata/app-sungeodata/test copy.ipynb Cell 6' in <cell line: 1>()
----> 1 ds_test  = xr.combine_by_coords([x2, x1])

File ~/Work/internal-applications/sungeodata/app-sungeodata/.venv/lib/python3.8/site-packages/xarray/core/combine.py:979, in combine_by_coords(data_objects, compat, data_vars, coords, fill_value, join, combine_attrs, datasets)
    977     concatenated_grouped_by_data_vars = []
    978     for vars, datasets_with_same_vars in grouped_by_vars:
--> 979         concatenated = _combine_single_variable_hypercube(
    980             list(datasets_with_same_vars),
    981             fill_value=fill_value,
    982             data_vars=data_vars,
    983             coords=coords,
    984             compat=compat,
    985             join=join,
    986             combine_attrs=combine_attrs,
    987         )
    988         concatenated_grouped_by_data_vars.append(concatenated)
    990 return merge(
    991     concatenated_grouped_by_data_vars,
    992     compat=compat,
   (...)
    995     combine_attrs=combine_attrs,
    996 )

File ~/Work/internal-applications/sungeodata/app-sungeodata/.venv/lib/python3.8/site-packages/xarray/core/combine.py:653, in _combine_single_variable_hypercube(datasets, fill_value, data_vars, coords, compat, join, combine_attrs)
    651     indexes = concatenated.indexes.get(dim)
    652     if not (indexes.is_monotonic_increasing or indexes.is_monotonic_decreasing):
--> 653         raise ValueError(
    654             f"Resulting object does not have monotonic"
    655             f" global indexes along dimension {dim} \n"
    656             f" indexes.is_monotonic_increasing {indexes.is_monotonic_increasing}"
    657             f" indexes.is_monotonic_decreasing {indexes.is_monotonic_decreasing}\n"
    658             f" concatenated[{dim}].values {concatenated[dim].values.tolist()}"
    659         )
    661 return concatenated

ValueError: Resulting object does not have monotonic global indexes along dimension x 
 indexes.is_monotonic_increasing False indexes.is_monotonic_decreasing False
 concatenated[x].values [10, 20, 30, 20, 30, 40]


### Anything else we need to know?

_No response_

### Environment

INSTALLED VERSIONS
------------------
commit: None
python: 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0]
python-bits: 64
OS: Linux
OS-release: 5.13.0-30-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.0
libnetcdf: 4.7.4

xarray: 2022.3.0
pandas: 1.4.1
numpy: 1.22.3
scipy: 1.6.2
netCDF4: 1.5.5.1
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.6.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: 0.9.8.4
iris: None
bottleneck: None
dask: 2022.02.1
distributed: 2022.2.1
matplotlib: 3.4.3
cartopy: None
seaborn: None
numbagg: None
fsspec: 2022.02.0
cupy: None
pint: None
sparse: None
setuptools: 44.0.0
pip: 20.0.2
conda: None
pytest: 7.0.1
IPython: 8.1.1
sphinx: 3.5.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with xarray/core/combine.py, particularly _combine_single_variable_hypercube and the monotonic-index check shown in the traceback. Reproduce the provided minimal example and inspect how combine_by_coords assembles the x indexes; done should match the expected 10, 20, 30, 40 layout without raising the monotonic-global-index error.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.