pydata / pydata/xarray

{DataArray,Dataset} accessors with parameters

Open
#3,829 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-documentation usage question
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

I would like to be able to create an DataArray accessor that takes parameters, e.g. obj.weighted(w).sum(dim). This appears to be impossible using the existing @register_{dataarray,dataset}_accessor, which supports only accessors of the form obj.weighted.sum(w, dim).

To support the desired syntax, one could simply change https://github.com/pydata/xarray/blob/master/xarray/core/extensions.py#L36
from

            accessor_obj = self._accessor(obj)

to

            accessor_obj = partial(self._accessor, obj)

But that would break the current syntax (i.e. would require obj.accessor().foo(), so is clearly not acceptable.

So any suggestions (short of simply creating slightly modified copies of register_{dataarray,dataset}_accessor) for supporting both the existing obj.accessor.foo() syntax as well as my desired obj.accessor(*args, **kwargs).foo() syntax?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in xarray/core/extensions.py at the accessor registration logic referenced in the issue. Investigate how to support both obj.accessor.foo() and obj.accessor(*args, **kwargs).foo() without breaking the existing syntax; done means both forms work for DataArray and Dataset accessors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.