pydata / pydata/xarray

Return multiple indexes from multi-coordinate index `isel`

Open
#10,477 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement topic-indexing
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

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        RasterIndex
  └ y

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Index.isel() API and trace how multi-coordinate selection handles its return value. Review the rasterix examples and linked context to understand the separate-index case; done means implementations can return per-coordinate indexes or drop them, producing PandasIndex objects for x and y in the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.