pydata / pydata/xarray

Unclear precedence between NumPy and JAX arrays in arithmetic

Open
#9,952 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

array API standard bug topic-arrays
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

Consider the following case of an xarray.DataArray wrapping a single element JAX array:

import jax
import xarray
import numpy as np

da = xarray.DataArray(jax.numpy.ones(1))

This object is wrapping a jax.Array, with operations implemented via the Array API (yay!), as one can check by inspect da.data.

da * 1 and 1 * da are both JAX arrays. So is da * np.array(1.0).

Unfortunately, np.array(1.0) * da is not -- it's a base NumPy array.

This feels quite inconsistent. Ideally JAX would take precedence in all these cases, even though the Python Array API rules technically do not prescribe an order of precendece between different array types.

What did you expect to happen?

No response

Minimal Complete Verifiable Example

No response

MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output

No response

Anything else we need to know?

No response

Environment
xarray = 2025.1.1 jax = 0.4.38

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 by reproducing the four arithmetic expressions in the issue with xarray.DataArray wrapping a JAX array, then trace xarray's arithmetic dispatch and array-protocol handling. Compare the operand-order results and existing tests around mixed NumPy and non-NumPy arrays; done means the intended precedence is documented and the inconsistent result is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
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.