Skip to content

DataArray.groupby.apply with a generic ndarray function #326

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
IamJeffG opened this issue Feb 19, 2015 · 1 comment · Fixed by #327
Closed

DataArray.groupby.apply with a generic ndarray function #326

IamJeffG opened this issue Feb 19, 2015 · 1 comment · Fixed by #327

Comments

@IamJeffG
Copy link
Contributor

Need to apply a transformation function across one dimension of a DataArray, where that non-xray function speaks in ndarrays. Currently the only ways to do this involve wrapping the function. An example:

import numpy as np
import xray
from scipy.ndimage.morphology import binary_opening

da = xray.DataArray(np.random.random_integers(0, 1, (10, 10, 3)),
                    dims=['row', 'col', 'time'])

# I want to apply an operation the 2D image at each point in time
da.groupby('time').apply(binary_opening)
# AttributeError: 'numpy.ndarray' object has no attribute 'dims'

def wrap_binary_opening(da, **kwargs):
    return xray.DataArray(binary_opening(da.values, **kwargs), da.coords)

da.groupby('time').apply(wrap_binary_opening)
da.groupby('time').apply(wrap_binary_opening, iterations=2) # func may take custom args

My proposed solution is that apply would automatically coerce func's return value to a DataArray.

@shoyer
Copy link
Member

shoyer commented Feb 20, 2015

Yes, this seems like a good idea to me!

@shoyer shoyer added this to the 0.4.1 milestone Feb 20, 2015
@shoyer shoyer changed the title DataArray.apply with a generic ndarray function DataArray.groupby.apply with a generic ndarray function Feb 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants