Skip to content

Groupby with observed=True doesn't sort #25167

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

Closed
topper-123 opened this issue Feb 5, 2019 · 1 comment
Closed

Groupby with observed=True doesn't sort #25167

topper-123 opened this issue Feb 5, 2019 · 1 comment
Labels
Bug Categorical Categorical Data Type Groupby
Milestone

Comments

@topper-123
Copy link
Contributor

topper-123 commented Feb 5, 2019

Code Sample, a copy-pastable example if possible

>>> df = pd.DataFrame({'A': pd.Categorical(['b', 'a']), 'B': [1, 2]})
>>> df.groupby('A').sum()  # ok
   B
A
a  2
b  1
>>> df.groupby('A', observed=True).sum()  # wrong sort order
   B
A
b  1
a  2

Problem description

By default groupby has sort=True, so should sort the groupings, also when using observed=True. In the above example, the sorting doesn't work together with observed=True.

Expected Output

>>> df.groupby('A', observed=True).sum()
   B
A
a  2
b  1

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.7.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.24.0
pytest: 4.0.1
pip: 18.1
setuptools: 40.4.2
Cython: None
numpy: 1.15.4
scipy: None
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: 2.5.9
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@jreback
Copy link
Contributor

jreback commented Apr 5, 2019

closed by #25908

@jreback jreback closed this as completed Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants