There appears to be an upstream Xarray bug for mixed integer/slice indexing ds.isel(x=slice(10), y=1). The following is Claude's explanation:
|
def is_mixed_scalar_slice_indexer(indexers: dict[Hashable, int | slice]) -> bool: |
|
# TODO: Fix bug in RasterIndex with mixed scalar/slice indexing across dimensions |
|
# When you have scalar indexing on one dimension (e.g., y=0) and slice indexing |
|
# on another (e.g., x=slice(None, 1)), RasterIndex.isel() returns None for the |
|
# scalar dimension, dropping that index. This causes xarray to incorrectly handle |
|
# the coordinate variables - the sliced dimension's coordinate (x) maintains |
|
# dims ('x',) even though the data has been reduced by the scalar indexing. |
|
# This results in: "ValueError: dimensions ('x',) must have the same length as |
|
# the number of data dimensions, ndim=0" |
|
# |
|
# Example failing case: raster_da.isel(y=0, x=slice(None, 1)) |
|
# - y=0 causes RasterIndex to return None for y dimension |
|
# - x=slice(None, 1) preserves RasterIndex for x dimension |
|
# - Result: coordinate variable x has wrong dimensionality |
|
# |
|
# For now, filter out these cases using hypothesis.assume() |
|
has_scalar = any(isinstance(v, int | np.integer) for v in indexers.values()) |
|
has_slice = any(isinstance(v, slice) for v in indexers.values()) |
|
return has_scalar and has_slice and len(indexers) > 1 |
cc @benbovy
There appears to be an upstream Xarray bug for mixed integer/slice indexing
ds.isel(x=slice(10), y=1). The following is Claude's explanation:rasterix/tests/test_indexing.py
Lines 26 to 44 in 0b84419
cc @benbovy