Skip to content

pipe, apply should call maybe_wrap_array, possibly resolve dim->axis #1130

Closed
@smartass101

Description

@smartass101

While pipe and Dataset.apply (btw, why not call them both the same?) specify that they expected DataArray returning functions, it would be very convenient to have them call maybe_wrap_array anyways.

I've often tried piping functions which at first looked like ufuncs only to find out that they forgot to call __array_wrap__ (I'm looking at you np.angle). The extra call to maybe_wrap_array is cheap, does not break anything and would be very useful. It would greatly enlarge the set of functions that can be readily applied to DataArray objects without any need for writing function wrappers (motivated in part by #1080).

Since many such functions expect an axis argument, some syntax for dim -> axis resolution could be also added. I see some options

  1. check if axis argument is a string and coerce it to a number, something like
axis = kwargs.get('axis')
if axis is not None:
    if isinnstance(axis, str):
        kwargs['axis'] = darray.get_axis_num(axis)

Simple, but specifying axis='smth' is not very explicit and may mean something else for certain funcs, it assumes a lot about function signatures.

  1. similar to 1., but only if both dim and axis='dim' are specified. Still possible conflict of specific meaning, but less likely.
if kwargs.get('axis') == 'dim':
    kwargs['axis'] = darray.get_axis_num(kwargs['dim'])

Other coding might be possible.

  1. use some syntax similar to pipe((f, 'arg2', ('axis', dim)), *args, **kwargs), but that's getting complicated and less readable.

Let me know what you think and perhaps you'll come up with some nicer syntax for dim-> axis resolution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions