[BUG] Groupby with as_index=False raises error when type is Category. · Issue #32599 · pandas-dev/pandas (original) (raw)

Code to reproduce the issue

import pandas as pd

test = pd.DataFrame([[1, 1], [2, 2], [3, 3]], columns=['col1', 'col2']) test['col1'] = test['col1'].astype('category')

test.groupby(['col1', 'col2'], as_index=False).size()

Problem description

With pandas 1.0.1, the code throws an error ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'>.
With pandas 0.25.3, the code works, but as_index argument do not function as already mentioned in #25011.
This happened with categorical type, the output of the new version is similar to 0.25.3 with other types.

Expected Output

col1 col2 0
0 1 1 1
1 1 2 0
2 1 3 0
3 2 1 0
4 2 2 1
5 2 3 0
6 3 1 0
7 3 2 0
8 3 3 1

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Darwin
OS-release : 19.3.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0.post20200210
Cython : None
pytest : 5.3.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : None
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.3.2
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : 0.8.6
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None