-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: df.groupby(sort=False) sorts multi-index-frames #17537
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
Comments
hmm this does look buggy. can you have a look? |
I have started debugging the problem but I have not found the error, yet. |
This bug seems to still be happening If you import pandas as pd
>>> df = pd.DataFrame([
[4, 2, 'x'],
[3, 1, 'y'],
],
columns=['A','B','C']).set_index(['A', 'B'])
C
A B
4 2 x
3 1 y
>>> df.groupby("A").size()
A
3 1
4 1
dtype: int64
>>> df.groupby("A", sort=False).size()
A
3 1
4 1
dtype: int64 Even though, if you use the >>> df.groupby(level="A", sort=False).size()
A
4 1
3 1
dtype: int64 Pandas 1.1.3 |
... and the bug still persists with python 3.9.13 and pandas 1.4.4. |
Code Sample, a copy-pastable example if possible
Problem description
DataFrame.groupby() has a parameter that selects whether the result should be sorted by groups.
However, if the DataFrame has a multi-index and the grouping is only done by one index, the result is sorted regardless of the value of
sort
.Grouping by more than one index works.
Passing the single index as a list
[0]
does not fix the problem.Expected Output
should yield
Output of
pd.show_versions()
commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.10.0-33-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL:
LANG: en_GB.UTF-8
LOCALE: de_DE.UTF-8
pandas: 0.21.0.dev+450.g6eadb87fe
pytest: None
pip: 9.0.1
setuptools: 36.0.1
Cython: None
numpy: 1.13.1
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: