Skip to content

cf.weighted doesn't work with DataArrays #226

Closed
@malmans2

Description

@malmans2

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions