Skip to content
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

Wrong dimension referenced in boolean indexing with .loc #1436

Closed
d-chambers opened this issue May 31, 2017 · 3 comments
Closed

Wrong dimension referenced in boolean indexing with .loc #1436

d-chambers opened this issue May 31, 2017 · 3 comments

Comments

@d-chambers
Copy link

I am using Ubuntu 16, python 3.6, and xarary 0.9.5.

import numpy as np
import xarray as xr

# setup for a simple grid
DX = 50
X = np.arange(0, 2010, DX)
Y = np.arange(0, 2010, DX)
Z = np.arange(0, 2010, DX)
grid_shape = (len(X), len(Y), len(Z))

# Create data array
dims = 'X Y Z'.split()
coords = {'X': X, 'Y': Y, 'Z': Z}
dar = xr.DataArray(np.ones(grid_shape), dims=dims, coords=coords)

# slice the data array so that all Z values are greater than 1000
dar2 = dar.loc[dar.Z > 1000]
assert np.all(dar2.Z > 1000)  # fails becase dar is sliced along X, not Z

Since the object returned from dar.Z > 1000 is a data array with "Z" as the only dim I would expect this to slice the "Z" dim rather than X.

@d-chambers
Copy link
Author

I should point out It does work when I specify the dimension with a dict:

dar2 = dar.loc[{'Z': dar.Z > 1000}]
assert np.all(dar2.Z > 1000)  # doesn't raise

@shoyer
Copy link
Member

shoyer commented May 31, 2017

Xarray's indexing currently ignores dimension/index labels, which I agree is not ideal (and can lead to surprising behavior like this): #974

Another work-around is to use where, e.g., dar.where(dar.Z > 1000).

@shoyer
Copy link
Member

shoyer commented Oct 19, 2017

Fixed by #1639

@shoyer shoyer closed this as completed Oct 19, 2017
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.

3 participants