Broadcasting doesn't respect scalar coordinates
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Usually if I apply a broadcasting operation to two arrays, the result only includes values for coordinates present in both. A simple example:
In [160]: data_array = xarray.DataArray(dims=("x",), data=[1,2,3], coords={"x": [1,2,3]})
In [161]: data_array.sel(x=[2]) * data_array
Out[161]:
<xarray.DataArray (x: 1)>
array([4])
Coordinates:
* x (x) int64 2
However if I do the same thing but select a scalar value for the x coordinate (.sel(x=1)) yielding a scalar coordinate for x:
In [164]: data_array.sel(x=2)
Out[164]:
<xarray.DataArray ()>
array(2)
Coordinates:
x int64 2
In [165]: data_array.sel(x=2) * data_array
Out[165]:
<xarray.DataArray (x: 3)>
array([2, 4, 6])
Coordinates:
* x (x) int64 1 2 3
Here the result includes values at all the coordinates [1,2,3], all of which have been broadcast against the scalar value taken at coordinate 2. This doesn't seem correct in general -- values from different coordinates shouldn't be broadcast against eachother by default, even if one of them is a scalar.
I would expect this either to result in an error or warning, or to select only the corresponding value at the scalar coordinate in question to broadcast against, resulting in e.g.:
<xarray.DataArray ()>
array(4)
Coordinates:
x int64 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the scalar-coordinate and one-element-coordinate examples from the issue, then trace xarray's broadcasting and coordinate-alignment entry points to determine where scalar coordinates are handled. Done means the behavior is explicitly resolved for this case—by an error, warning, or coordinate-aware result—and the examples have regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100