Skip to content

Fix/tensor indexing bug #936#941

Merged
sandorkertesz merged 6 commits intorelease/1.0.0rc0from
fix/tensor-indexing-bug-936
Mar 23, 2026
Merged

Fix/tensor indexing bug #936#941
sandorkertesz merged 6 commits intorelease/1.0.0rc0from
fix/tensor-indexing-bug-936

Conversation

@pawel-wolff
Copy link
Contributor

Description

This PR fixes the issue #936.

The bug was caused by the recent upgrade (#932) of the earthkit tensor indexing support from BASIC to OUTER (see https://docs.xarray.dev/en/stable/internals/how-to-add-new-backend.html#indexing-examples).

With the OUTER indexing support, xarray can ask the earthkit backend to sub-select a tensor axis using:

  • a single integer
  • a slice
  • an array of integers or a boolean mask array

The last kind of indexer turned out to be problematic. More specifically, if a field part v of a tensor is a 2D lat-lon array and one wants to subselect it using a pair of indexers of the third kind (e.g. index=([9, 10, 11], [0, 1]), see:

da[:, 0, :, 2, [9, 10, 11], [0, 1]],
), then the numpy-style indexing v[index] ( )
does not work, because it applies the so-called "advanced indexing" (https://docs.xarray.dev/en/stable/internals/how-to-add-new-backend.html#indexing-examples).

In order to solve this problem, an outer indexing is applied explicitly to sub-select a field in the following methods:

Note that in the case of similar methods:

  • NumpyNDArrayWrapper.to_numpy()
  • XarrayDataArrayData.to_numpy()

the indexing for a field is still v[index], so the usual numpy-style indexing applies (which might be an advanced indexing, depending on index).

Several tests has been added or extended.

Contributor Declaration

By opening this pull request, I affirm the following:

  • All authors agree to the Contributor License Agreement.
  • The code follows the project's coding standards.
  • I have performed self-review and added comments where needed.
  • I have added or updated tests to verify that my changes are effective and functional.
  • I have run all existing tests and confirmed they pass.

@sandorkertesz
Copy link
Collaborator

@pawel-wolff, thank you for this great improvement.

@sandorkertesz sandorkertesz merged commit 7c90a88 into release/1.0.0rc0 Mar 23, 2026
111 of 138 checks passed
@sandorkertesz sandorkertesz deleted the fix/tensor-indexing-bug-936 branch March 23, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants