pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.48k stars 1.04k forks source link

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

Open Tonow opened 2 years ago

Tonow commented 2 years ago

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
Tonow commented 2 years ago

For information:

they is a problem only for the second dimension this example works well

6| x4  x4   x4
5| x4  x4   x4
4| x4  x4   x4
3|     x3   x3   x3
2|     x3   x3   x3
1|     x3   x3   x3  
  ----------------
   10  20  30  40
x3 = xr.Dataset(
    {

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

ds_test  = xr.combine_by_coords([x3, x4])
ds_test.temperature.plot()
mathause commented 2 years ago

Potentially related: #4824 (especially https://github.com/pydata/xarray/issues/4824#issuecomment-841244252)