id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 1674456910,I_kwDOAMm_X85jzi9O,7766,groupby_bins incorrect correspondence with labels,16255489,closed,0,,,1,2023-04-19T08:44:49Z,2023-04-20T17:17:20Z,2023-04-20T17:17:20Z,NONE,,,,"### What happened? As of version 2023.4.0 groupby_bins stopped giving correct results. This was reported in #7751. The fix applied in 2023.4.1 changes the behaviour but does not fix the problem in my use case where strings are used as labels for the groups. It appears that the sorting that was implemented for the fix sorts based on name (so e.g. 'one' comes after 'four', see example) instead of on the lower edge. I could be wrong about the reason but accessing groups like data[group['label']] breaks ### What did you expect to happen? Until 2023.3.0 accessing groups by name gave the correct subset from the DataArray ### Minimal Complete Verifiable Example ```Python import numpy as np import xarray as xr import pandas as pd import matplotlib.pyplot as plt import sys print(f""numpy version: {np.__version__}"") print(f""xarray version: {xr.__version__}"") print(f""pandas version: {pd.__version__}"") print(f""python version: {sys.version}"") # Generate random data # Make the coordiantes follow a normal distribution np.random.seed(42) coords = np.random.normal(5, 5, 1000) bins = np.logspace(-4, 1, 10) labels = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] # xArray # Make a mock dataarray darr = xr.DataArray(coords, coords=[coords], dims=[""coords""]) groups_xr = darr.groupby_bins(""coords"", bins, labels=labels).groups print(groups_xr.keys()) for lab in labels: print(lab, end=' ') try: print(int(darr[groups_xr[lab]].count())) except: print(""nan"") ``` ### MVCE confirmation - [x] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray. - [x] Complete example — the example is self-contained, including all data and the text of any traceback. - [X] Verifiable example — the example copy & pastes into an IPython prompt or [Binder notebook](https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/blank_template.ipynb), returning the result. - [X] New issue — a search of GitHub Issues suggests this is not a duplicate. ### Relevant log output _No response_ ### Anything else we need to know? Output from 2023.3.0 (keys in random order, but correct access by name) numpy version: 1.23.5 xarray version: 2023.3.0 pandas version: 1.5.3 python version: 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] dict_keys(['nine', 'eight', 'seven', 'five', 'six', 'four']) one nan two nan three nan four 1 five 2 six 9 seven 27 eight 153 nine 506 Output from 2023.4.1 - Keys alphabetically sorted, wrong access by name numpy version: 1.23.5 xarray version: 2023.4.1 pandas version: 1.5.3 python version: 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] dict_keys(['eight', 'five', 'four', 'nine', 'seven', 'six']) one nan two nan three nan four 9 five 2 six 506 seven 153 eight 1 nine 27 ### Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] python-bits: 64 OS: Linux OS-release: 5.19.0-40-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: en_US.UTF-8 LANG: el_GR.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.10.7 libnetcdf: 4.8.1 xarray: 2023.4.1 pandas: 1.5.3 numpy: 1.23.5 scipy: 1.10.2.dev0+2411.f2ee108 netCDF4: 1.5.8 pydap: None h5netcdf: 1.1.0 h5py: 3.6.0 Nio: None zarr: 2.14.2 cftime: 1.5.2 nc_time_axis: None PseudoNetCDF: None iris: None bottleneck: 1.3.7 dask: 2023.4.0 distributed: 2023.4.0 matplotlib: 3.7.1 cartopy: 0.21.1 seaborn: 0.12.2 numbagg: None fsspec: 2023.4.0 cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 67.6.1 pip: 23.1 conda: None pytest: 6.2.5 mypy: 0.942 IPython: 8.12.0 sphinx: 4.3.2
","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/7766/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue