Skip to content

name of array disappear when apply "where" filter with Datarray but not with np.array #2457

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
svicario opened this issue Oct 2, 2018 · 2 comments · Fixed by #2749
Closed

Comments

@svicario
Copy link

svicario commented Oct 2, 2018

I was trying to select by non dimensional coordinate, and I found that "where" works well, but it erase the name of my datarray only if filtering is done with another datarray as a non dimensional coordinate

import xarray as xr
temp = 15 + 8 * np.random.randn(2, 2, 3)

precip = 10 * np.random.rand(2, 2, 3)

lon = [[-99.83, -99.32], [-99.79, -99.23]]

lat = [[42.25, 42.21], [42.63, 42.59]]
ds = xr.Dataset({'temperature': (['x', 'y', 'time'],  temp)},
                 coords={'lon': (['x', 'y'], lon),
                         'lat': (['x', 'y'], lat),
                         'precipitation': (['x', 'y', 'time'], precip),
                         'time': pd.date_range('2014-09-06', periods=3),
                         'reference_time': pd.Timestamp('2014-09-05')})

ds["temperature"].where(ds.precipitation>5).sum(axis=2).plot()

this line would produce a label near color bar
but only using a numpy array this happens as here

ds["temperature"].where(ds.precipitation.values>5).sum(axis=2).plot()

Expected Output

I think that selecting with a datarray should not change the label of the data.
Further I was expecting that as for "sel", "where" should change the title of the graph
so that I know from the title that I performed a selection with where with a non dimensional coordinate

@shoyer
Copy link
Member

shoyer commented Oct 2, 2018

We currently drop names because that's our default behavior for xarray operations with multiple named arguments. But the where method uses apply_ufunc() with keep_attrs=True, which indicates that attributes from the first object should be kept. It would probably make sense to keep the name of the first object, too, when using keep_attrs=True. That would fix this issue and also fix the problem for other xarray methods (e.g., fillna) that use the same logic internally.

@max-sixty
Copy link
Collaborator

Confirmed. To show the specific result:

ds['temperature'].name
# 'temperature'

ds["temperature"].where(ds.precipitation>5).name
# None

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