question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ordered Groupby Keys

See original GitHub issue

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:

plot_kwargs = dict(col='season', vmin=15, vmax=35, levels=12, extend='both')

da_obs = ds_obs.SALT.isel(depth=0).groupby('time.season').mean('time')
da_obs.plot(**plot_kwargs)

index Note that MAM and JJA are out of order.

I think this could be easily fixed by using an OrderedDict in xarray.core.Groupby.groups.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dcheriancommented, Nov 20, 2019

I use reindex instead of sortby

0reactions
jbuseckecommented, Nov 20, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I sort the groupBy Collection keys? - Laracasts
Hi, I have a collection I wanted to group by a category, like so: $my_objects->groupBy(function ($item, $key) { if ($item->category) { return ...
Read more >
Sorting LINQ 'GroupBy' operator keys into a custom order
In c#, when using the LINQ operator 'GroupBy' I would like to sort the keys into a specified custom ...
Read more >
DOC: Order of groups in groupby and head method #17775
When sort = True is passed to groupby (which is by default) the groups will be in sorted order. If you loop through...
Read more >
GroupBy key a sequence of ordered key values
I am working on haskell, under the hood I have a Seq that is a Vector, but a pseudocode solution is OK. I...
Read more >
Pandas Groupby Sort within Groups - Spark by {Examples}
By using DataFrame.sort_values() , you can sort DataFrame in ascending or descending order, before you use this first group the DataFrame rows by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found