Skip to content

Return multiple indexes from multi-coordinate index isel #10477

Open
@benbovy

Description

@benbovy

Is your feature request related to a problem?

Let's consider the following DataArray with a rasterix RasterIndex associated to the x/y coordinates:

>>> da
<xarray.DataArray 'band_data' (y: 332, x: 316)> Size: 420kB
[104912 values with dtype=float32]
Coordinates:
  * x            (x) float64 3kB -3.938e+06 -3.912e+06 ... 3.912e+06 3.938e+06
  * y            (y) float64 3kB 4.338e+06 4.312e+06 ... -3.912e+06 -3.938e+06
Indexes:
  ┌ x        RasterIndexy

Data selection on x and y at arbitrary array positions will drop the raster index since it is built around affine coordinate transformation:

>>> da2 = da.isel(x=[10, 31], y=[0, 4])
>>> da2
<xarray.DataArray 'band_data' (y: 2, x: 2)> Size: 16B
[4 values with dtype=float32]
Coordinates:
    x            (x) float64 16B -3.688e+06 -3.162e+06
    y            (y) float64 16B 4.338e+06 4.238e+06
>>> da2.xindexes
Indexes:
    *empty*

It would be convenient if instead default (pandas) indexes were created for those coordinates in the result:

>>> da2
<xarray.DataArray 'band_data' (y: 2, x: 2)> Size: 16B
[4 values with dtype=float32]
Coordinates:
  * x            (x) float64 16B -3.688e+06 -3.162e+06
  * y            (y) float64 16B 4.338e+06 4.238e+06
>>> da2.xindexes
Indexes:
    x        PandasIndex
    y        PandasIndex

This is not currently possible, though, since xarray.Index.isel() returns either one Index object or None.

Describe the solution you'd like

Change Xarray's Index API from:

class Index:

    def isel(...) -> Index | None:

to

class Index:

    def isel(...) -> Index | dict[Hashable, Index | None] | None:

So that it is possible to return different index objects (or drop the index) for each coordinate variable associated with the index.

Describe alternatives you've considered

In the example above, one alternative would be to encapsulate PandasIndex instances within rasterix's RasterIndex. This would make the implementation of the latter much more complicated, though.

Additional context

https://github.com/dcherian/rasterix/issues/50

https://github.com/dcherian/rasterix/pull/43

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions