Skip to content

Instantly share code, notes, and snippets.

@shoyer
Created February 19, 2015 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoyer/c16b1f9ec4f7c0de9e64 to your computer and use it in GitHub Desktop.
Save shoyer/c16b1f9ec4f7c0de9e64 to your computer and use it in GitHub Desktop.
xray nd-groupby
import functools
def nd_groupby_apply(ds, dims, func):
if isinstance(dims, str):
dims = [dims]
if len(dims) > 1:
func = functools.partial(nd_groupby_apply, dims=dims[1:], func=func)
return ds.groupby(dims[0]).apply(func)
def nd_group_over_apply(ds, dims, func):
if isinstance(dims, str):
dims = [dims]
by_dims = [d for d in ds.dims if d not in dims]
return nd_groupby_apply(ds, by_dims, func)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment