Skip to content
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

workaround to implement groupby_bins along dimension #6450

Closed
nicrie opened this issue Apr 7, 2022 · 1 comment
Closed

workaround to implement groupby_bins along dimension #6450

nicrie opened this issue Apr 7, 2022 · 1 comment
Labels
plan to close May be closeable, needs more eyeballs

Comments

@nicrie
Copy link
Contributor

nicrie commented Apr 7, 2022

What is your issue?

There are several related issues dealing with groupby operations along individual/multiple dimensions (e.g. #1056 #924 ). I understand that this is work in progress. In the meantime, however, I'm looking for a quick solution to group over bins along a single dimension. Grouping over all dimensions works, e.g.:

>>> da = xr.DataArray(np.random.randint(9, size=(5, 2, 3)), dims=['time', 'x', 'y'])
>>> da

xarray.DataArraytime: 5x: 2y: 3
array([[[1, 1, 5],
        [5, 6, 0]],

       [[3, 2, 4],
        [2, 8, 4]],

       [[0, 0, 2],
        [6, 3, 5]],

       [[4, 6, 4],
        [8, 2, 4]],

       [[6, 5, 3],
        [5, 2, 6]]])
Coordinates: (0)
Attributes: (0)

>>> da.groupby_bins(da, bins=[0, 5, 10]).count()

xarray.DataArray 'group' group_bins: 2
array([20,  7])
Coordinates: (1)
Attributes: (0)

However, it fails when trying to apply the operator only along a specific dimension, e.g.:

>>> da.groupby_bins(da, bins=[0, 5, 10]).count('time')

ValueError: cannot reduce over dimensions ['time']. expected either '...' to reduce over all dimensions or one or more of ('stacked_time_x_y',).

I don't mind if the workaround is a bit ugly as long as it works ;)

@nicrie nicrie added the needs triage Issue that has not been reviewed by xarray team member label Apr 7, 2022
@dcherian
Copy link
Contributor

dcherian commented Apr 7, 2022

Can you please try xhistogram or flox?

@dcherian dcherian added plan to close May be closeable, needs more eyeballs and removed needs triage Issue that has not been reviewed by xarray team member labels Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to close May be closeable, needs more eyeballs
Projects
None yet
Development

No branches or pull requests

2 participants