Skip to content

support mixed int/slice indexing #58

@dcherian

Description

@dcherian

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions