home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 594670062

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/3829#issuecomment-594670062 https://api.github.com/repos/pydata/xarray/issues/3829 594670062 MDEyOklzc3VlQ29tbWVudDU5NDY3MDA2Mg== 14808389 2020-03-04T17:10:59Z 2020-03-04T17:15:52Z MEMBER

not sure if you really should use accessors to do that; having a function return a wrapper object might be enough.

Then again, an accessor is a slightly modified version of a property so you can just make your object callable by defining __call__: ```python @xr.register_dataarray_accessor("weighted") class Weighted: def init(self, xarray_obj): self._obj = xarray_obj self._weight = None

def __call__(self, weight):
    self._weight = weight

def sum(self, dim):
    return "weighted sum"

this does allow calling `obj.weighted.sum(dim)`, so instead you can use:python In [2]: class Weighted: ...: def init(self, obj, weight): ...: self._obj = obj ...: self._weight = weight ...:
...: def sum(self, dim): ...: return f"weighted sum over {dim} and with weight {self._weight}" ...:
...: @xr.register_dataarray_accessor("weighted") ...: def weighted(obj): ...: def wrapped(weight): ...: return Weighted(obj, weight) ...: return wrapped ...:
...: da = xr.DataArray(data=range(5), dims="x") ...: da.weighted(5).sum(dim="x") Out[2]: 'weighted sum over x and with weight 5' ```

Edit: the warning about a overridden attribute was the result of rerunning the code without restarting the interpreter

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  575564170
Powered by Datasette · Queries took 0.826ms · About: xarray-datasette