Skip to content

vectorized indexing with DataArray should not preserve IndexVariable #6381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dcherian opened this issue Mar 19, 2022 · 1 comment · Fixed by #6388
Closed

vectorized indexing with DataArray should not preserve IndexVariable #6381

dcherian opened this issue Mar 19, 2022 · 1 comment · Fixed by #6388

Comments

@dcherian
Copy link
Contributor

dcherian commented Mar 19, 2022

What happened?

After vectorized indexing a DataArray with dim xby a DataArray z, we get a DataArray with dim z and x as non-dim coordinate. But x is still an IndexVariable, not a normal variable.

What did you expect to happen?

x should be a normal variable.

Minimal Complete Verifiable Example

import xarray as xr
xr.set_options(display_style="text")

da = xr.DataArray([1, 2, 3], dims="x", coords={"x": [0, 1, 2]})
idxr = xr.DataArray([1], dims="z", name="x", coords={"z": ("z", ["a"])})
da.sel(x=idxr)
<xarray.DataArray (z: 1)>
array([2])
Coordinates:
    x        (z) int64 1
  * z        (z) <U1 'a'

x is a non-dim coordinate but is backed by a IndexVariable with the wrong name!

da.sel(x=idxr).x.variable
<xarray.IndexVariable 'z' (z: 1)>
array([1])

Relevant log output

No response

Anything else we need to know?

No response

Environment

xarray main but this bug was present prior to the explicit indexes refactor.

@benbovy
Copy link
Member

benbovy commented Mar 20, 2022

Good catch @dcherian. I think #6388 will fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants