xarray groupby multiple variables #5442
-
Hi there, I have the following xarray dataset:
I would like to do something like in xarr_ds.groupby(["U", "V"]) - like in dask. However, this is not possible in dask because the cells cannot be a matrix. Also, this is not possible in xarray as well since one can only groupby by only one variable. Is there a workaround for this? Has anyone done a multiple variable groupby on a xarray dataset? This is what I am trying to do:
Cheers |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
This is something we'd really like to improve, and some efforts (e.g. numpy_groupies) will move us closer. There are some approaches that will allow for multiple-dimension groupbys now (edit: much of the time xarray handles this already, and doesn't require manually stacking-groupby-unstacking) — and to respond quickly rather than completely — the approach I normally take is to stack the relevant dimensions, run the groupby operation, and then unstack. @miguelcarcamov could you make your example copy-pastable per #5404? |
Beta Was this translation helpful? Give feedback.
-
Yep @max-sixty, I have been checking that stack is probably the solution, although I don't get how to do it on my case. I have added the code to my question. Another question is: It will work if I do |
Beta Was this translation helpful? Give feedback.
-
UPDATE: However, this takes a lot of time and uses a lot of RAM (more than 8GB) since the line cannot end because it fills the whole memory. |
Beta Was this translation helpful? Give feedback.
-
Now possible with flox: https://flox.readthedocs.io/en/latest/intro.html#grouping-by-multiple-variables |
Beta Was this translation helpful? Give feedback.
Now possible with flox: https://flox.readthedocs.io/en/latest/intro.html#grouping-by-multiple-variables