Open jhamman opened 8 years ago
I agree this is annoying, but I don't think your diagnosis is correct here. The groups
property isn't used by any internal routines AFAICT. The issue is that groups are sorted, but as text rather than ordered categorical -- notice that the labels are ordered alphabetically.
Hmmm, a mystery. I'll look into this a bit more.
For what it's worth, I don't think we have any good solutions short of adding our own array type do to Categorical in xarray. We could set sort=False in some cases when we call pd.factorize but that's not a great alternative.
Ahh, so it's sorted, instead of keeping the original order.
I was expecting DataArray.groupby().reduce
would work like np.apply_along_axis
, and used the data
of the result directly.
Just stumbled across this issue. Is there a recommended workaround?
I am usually doing this (specific to seasons):
import xarray as xr
ds = xr.tutorial.open_dataset('air_temperature')
airtemp_seasonal = ds.groupby('time.season').mean('time').sortby(xr.DataArray(['DJF','MAM','JJA', 'SON'],dims=['season']))
Thought this might help some folks who need to solve this problem.
I use reindex
instead of sortby
Closing as stale, please reopen with an updated summary if relevant
Hehe, I'm so close to actually fixing this! :)
The current behavior of the xarray's
Groupby.groups
property provides a standard (unordered) dictionary. This is fine for most cases but leads to odd orderings in use cases like this one where I am using xarray's FacetGrid plotting:I think this could be easily fixed by using an
OrderedDict
inxarray.core.Groupby.groups
.