Description
What happened?
I'm struggling whether this is a bug or not. At least I faced a very unexpected behaviour.
For two given data arrays a
and b
with same dimensions and equal coordinates, c
for c = a.where(b)
should have equal dimensions and coordinates.
However if the coordinates of a
have dtype of float32 and those of b
are float64, then the dimension sizes of c
will always be two. Of course, this way the coordinates of a
and b
are no longer exactly equal, but from a user perspective they represent the same labels.
The behaviour is likely caused by the fact that the indexes generated for the coordinates are no longer strictly equal, therefore where()
picks only the two outer cells of each dimension. Allowing to explicitly pass indexes may help here, see #6392.
What did you expect to happen?
In the case described above, the dimensions and coordinates of c
should be equal to a
(and b
).
Minimal Complete Verifiable Example
import numpy as np
import xarray as xr
c32 = xr.DataArray(np.linspace(0, 1, 10, dtype=np.float32), dims='x')
c64 = xr.DataArray(np.linspace(0, 1, 10, dtype=np.float64), dims='x')
c3 = c32.where(c64 > 0.5)
assert len(c32) == len(c3)
v32 = xr.DataArray(np.random.random(10), dims='x', coords=dict(x=c32))
v64 = xr.DataArray(np.random.random(10), dims='x', coords=dict(x=c64))
v3 = v32.where(v64 > 0.5)
assert len(v32) == len(v3)
# --> Assertion error, Expected :10, Actual :2
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.
Relevant log output
No response
Anything else we need to know?
No response
Environment
Metadata
Metadata
Assignees
Type
Projects
Status