BUG: Should be able to group by a categorical Series of unequal length · Issue #44179 · pandas-dev/pandas (original) (raw)

Reproducible Example

import pandas as pd series = pd.Series(range(7)) grouper = pd.Series(list("ABBA"), dtype="category") series.groupby(grouper)

Raises ValueError: Length of grouper (4) and axis (7) must be same length

Issue Description

When grouping by a Series, the values of the series after alignment determine the groups. In the example, however, pandas does not attempt to align the series and instead unnecessarily wants it to have the same length as the axis of grouping.

Expected Behavior

I expect obj.groupby(grouper), where grouper is a Series with a categorical data type, to allow the grouper to have unequal length and to align it, as in the following where the grouper has a non-categorical data type:

series.groupby(grouper.astype(object)).groups

Returns {'A': [0, 3], 'B': [1, 2]}

Installed Versions

INSTALLED VERSIONS

commit : 9b2bb73
python : 3.8.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17134
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252

pandas : 1.4.0.dev0+952.g9b2bb732f0
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : 0.29.24
pytest : 6.2.5
hypothesis : 6.23.1
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : 3.0.1
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.28.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
fsspec : 2021.10.0
fastparquet : 0.7.1
gcsfs : 2021.10.0
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : 2021.10.0
scipy : 1.7.1
sqlalchemy : 1.4.25
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.18.2
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1