Skip to content

GroupBy(..., as_index=True).agg() drops index when  #28641

Closed
@adamhooper

Description

@adamhooper

Code Sample, a copy-pastable example if possible

In [1]: import pandas as pd

In [2]: pd.DataFrame({"A": [1997], "B": pd.Series(["b"], dtype="category").cat.as_ordered()}).groupby("A", as_index=True).agg({"B": "size"})
Out[2]:
      B
A
1997  1

In [3]: pd.DataFrame({"A": [1997], "B": pd.Series(["b"], dtype="category").cat.as_ordered()}).groupby("A", as_index=True).agg({"B": "min"})
Out[3]:
   B
0  b

Problem description

When aggregating min, max or first of a categorical column, .agg() returns a dataframe with a default index instead of the index returned by groupby().

(This may be related to #13416 ... but I think it's a clear, well-defined bug so maybe easier to resolve?)

In my case, I think I can work around this problem with a hack: if I .agg({"B": ["min", "size"]}) and then ignore the ("B", "size") output column, Pandas will output a dataframe with the correct index.

Expected Output

Out[3]:
      B
A
1997  b

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.4.final.0 python-bits : 64 OS : Linux OS-release : 5.2.11-200.fc30.x86_64 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 0.25.0
numpy : 1.16.1
pytz : 2018.9
dateutil : 2.8.0
pip : 19.0.3
setuptools : 40.8.0
Cython : 0.29.5
pytest : 4.5.0
hypothesis : None
sphinx : 2.1.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.2.5
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : 7.2.0
pandas_datareader: None
bs4 : 4.6.3
bottleneck : None
fastparquet : 0.2.1
gcsfs : None
lxml.etree : 4.2.5
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.14.1
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    GroupbyNeeds TestsUnit test(s) needed to prevent regressions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions