BUG: Incorrect index with groupby
and groupby.agg
when observed=False
using categorical columns with/without as_index=False
#46492
Labels
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
When using cateogry types as gb columns with as_index=True
Both
df.groupby
anddf.groupby.agg
work but the result seems to have all permutations of the underyling categories.See output of reproducible example below
When using cateogry types as gb columns with as_index=False
df.groupby
works but again the result seems to have all permutations of the underyling categories.However,
df.groupby.agg
fails with a crpytic error message likeValueError: Length of values (3) does not match length of index (9)
Expected Behavior
Both
groupby
andgroupby.agg
should work with/withoutas_index
and shouldn't do a cross join on the underlying categoriesInstalled Versions
INSTALLED VERSIONS
commit : 66e3805
python : 3.7.13.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.3.5
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.2
pip : 22.0.4
setuptools : 57.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3 (dt dec pq3 ext lo64)
jinja2 : 3.0.1
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.07.0
fastparquet : None
gcsfs : None
matplotlib : 3.5.0
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 4.0.1
pyxlsb : None
s3fs : None
scipy : 1.7.0
sqlalchemy : 1.4.32
tables : None
tabulate : 0.8.9
xarray : None
xlrd : 1.1.0
xlwt : None
numba : 0.53.1
The text was updated successfully, but these errors were encountered: