Skip to content

Accept lambda in methods with one obvious xarray argument #3825

Closed
@max-sixty

Description

@max-sixty

Branching from #3770

Here's the proposal: allow lambdas on methods where the primary argument is a single xarray object, and interpret lambas as though they'd be supplied in a pipe method followed by the current method.
Taking the example from the linked issue:

In [1]: import xarray as xr                                                                                                                                         

In [2]: import numpy as np                                                                                                                                          

In [3]: da = xr.DataArray(np.random.rand(2,3))                                                                                                                      

In [4]: da.where(da > 0.5)                                                                                                                                          
Out[4]: 
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[       nan, 0.71442406,        nan],
       [0.55748705,        nan,        nan]])
Dimensions without coordinates: dim_0, dim_1

# this should be equivalent (currently not valid)

In [5]: da.where(lambda x: x > 0.5)

# the longer version (currently works)

In [5]: da.pipe(lambda x: x.where(x > 0.5))
                                                                                                                                 

Others I miss from pandas: assign, and loc.
I haven't gone through the list though assume there are others; we don't have to agree 100% on the list before starting with the most obvious ones, assuming we're in agreement with the principle.

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