ValueError index must be monotonic increasing or decreasing when Indexing zarr Dataset with sel() Method

When attempting to index a zarr dataset, constructed from two timestamp tiles of Sentinel2 imagery, using xarray’s sel() method, I encounter a ValueError stating that the index must be monotonic increasing or decreasing. Intriguingly, reversing the order of the tiles when creating the zarr dataset and subsequently indexing it resolves the problem. The error may not stem directly from issues related to the dataset’s time dimension. Instead, it appears more likely related to how NaN values in the x dimension are handled.

Steps to Reproduce

  1. Create a dataset from two timestamp tiles of Sentinel2.
  2. Attempt to index the dataset for a specific coordinate using the sel() method.
  3. Encounter the ValueError regarding non-monotonic indices.

Expected Behavior

The sel() method should successfully index the dataset without requiring the indices to be in a strictly monotonic order, or it should handle non-monotonic indices in a more intuitive way.

Actual Behavior

A ValueError is raised, stating:

 ValueError: index must be monotonic increasing or decreasing

Environment

Python Version: 3.9.7
Xarray Version: 2024.2.0
Pandas Version: 2.1.1
NumPy Version: 1.24.3
Zarr Version: 2.16.1

Additional Information

  • The issue occurs irrespective of the dataset’s time dimension, as tested with other images.
  • Reversing the order of the Sentinel2 tiles allows the sel() method to index the data successfully.
    Adding screenshots of the zarr dataset structures (both in the original and reversed order) are included:-

Orignial tile order dataset:-

Reverse tile order dataset:-
I can only add one media file, so I will add the reverse screenshot in the thread.

Seeking Suggestions

  • Is there a recommended approach to handle or preprocess datasets in xarray to avoid this error?
  • Could NaN values within the dataset influence the monotonicity check, and if so, how can they be effectively managed?

I appreciate any insights or recommendations on how to address this issue.

Reverse tile order dataset:-

Thanks @Jain_Dimple - Looks like you-cross posted this on Xarray Discussions, I added a reply over there ValueError index must be monotonic increasing or decreasing when Indexing zarr Dataset with sel() Method · pydata/xarray · Discussion #8812 · GitHub

Thank You for your response @scottyhq