Skip to content

Accept lambda in methods with one obvious xarray argument #3825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
max-sixty opened this issue Mar 4, 2020 · 3 comments
Closed

Accept lambda in methods with one obvious xarray argument #3825

max-sixty opened this issue Mar 4, 2020 · 3 comments

Comments

@max-sixty
Copy link
Collaborator

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.

@max-sixty
Copy link
Collaborator Author

assign is already supported! 🤦‍♂

@max-sixty
Copy link
Collaborator Author

where is complete

I think the others that could be worthwhile are the selection methods:

  • sel & isel
  • loc
  • reindex?
    ...but much of their use case is bool arrays, which is mostly covered by where(..., drop=True).

Interested in what others think. I'm in no rush to implement those; where seemed much clearer to me.

@max-sixty
Copy link
Collaborator Author

Closing; can do others with the benefit of more experience

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant