Skip to content

Support groupby for multiple coordinates along the same dimension #6044

Answered by dcherian
raj-magesh asked this question in Ideas
Discussion options

You must be logged in to vote
import xarray as xr

d = xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
    coords={
        'a': ('dim1',['A', 'A', 'B', 'B']),
        'b': ('dim1',['1', '2', '1', '2']),
        'c': ('dim2',['x', 'y', 'z'])
    },
    dims=['dim1', 'dim2'])
d
<xarray.DataArray (dim1: 4, dim2: 3)>
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
Coordinates:
    a        (dim1) <U1 'A' 'A' 'B' 'B'
    b        (dim1) <U1 '1' '2' '1' '2'
    c        (dim2) <U1 'x' 'y' 'z'
Dimensions without coordinates: dim1, dim2

Does flox do what you want?

import flox.xarray
flox.xarray.xarray_reduce(d, "a", "b", func="sum")
<xarray.DataArray (dim2: 3, a: 2, b: 2)>
…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@raj-magesh
Comment options

@rsignell-usgs
Comment options

@keewis
Comment options

@dcherian
Comment options

Answer selected by raj-magesh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
4 participants