Skip to content

cf.weighted doesn't work with DataArrays #226

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
malmans2 opened this issue May 20, 2021 · 1 comment · Fixed by #227
Closed

cf.weighted doesn't work with DataArrays #226

malmans2 opened this issue May 20, 2021 · 1 comment · Fixed by #227

Comments

@malmans2
Copy link
Member

Looks like .cf.weighted breaks with DataArrays.

from cf_xarray.datasets import airds
weights = xr.ones_like(airds["air"])
weights.attrs = {}
airds.cf.weighted(weights)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Note that I can't do this, because only Datasets and DataArrays have the .cf accessor:

airds.weighted(weights).cf.mean("T")
AttributeError: 'DatasetWeighted' object has no attribute 'cf'

One possible workaround is:

airds.weighted(weights).mean(airds.cf.axes["T"])

or

airds = airds.assign_coords(weights=weights)
airds["air"].attrs["cell_measures"] += " weights: weights"
airds.cf.weighted("weights").mean("T")

but I don't think that's what we want.

@dcherian
Copy link
Contributor

airds.cf.weighted(weights)

I think we could fix this in the mapper. Pretty sure we only test airds.cf.weighted('area')

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

Successfully merging a pull request may close this issue.

2 participants