Skip to content

GroupBy aggregation fails if DataFrame has CategoricalIndex  #31223

Closed
@frances-h

Description

@frances-h

Code Sample, a copy-pastable example if possible

ids = pd.Categorical([0, 1, 2])
df = pd.DataFrame({
    'id': ids,
    'groups': [1, 1, 2],
    'value': [0, 1, 0]
}).set_index('id')

df.groupby('groups').agg({'value': pd.Series.nunique})

Problem description

The above works in v. 0.25.3 but not in 1.0.0rc0. It fails with: TypeError: Cannot convert Categorical to numpy.ndarray

Full stack trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/groupby/generic.py", line 940, in aggregate
    result, how = self._aggregate(func, *args, **kwargs)
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/base.py", line 430, in _aggregate
    result = _agg(arg, _agg_1dim)
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/base.py", line 397, in _agg
    result[fname] = func(fname, agg_how)
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/base.py", line 381, in _agg_1dim
    return colg.aggregate(how)
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/groupby/generic.py", line 265, in aggregate
    return self._python_agg_general(func, *args, **kwargs)
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 935, in _python_agg_general
    result, counts = self.grouper.agg_series(obj, f)
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/groupby/ops.py", line 624, in agg_series
    return self._aggregate_series_fast(obj, func)
  File "~/.virtualenvs/3.6-test-pd-1.0.0/lib/python3.6/site-packages/pandas/core/groupby/ops.py", line 648, in _aggregate_series_fast
    grouper = libreduction.SeriesGrouper(obj, func, group_index, ngroups, dummy)
  File "pandas/_libs/reduction.pyx", line 329, in pandas._libs.reduction.SeriesGrouper.__init__
TypeError: Cannot convert Categorical to numpy.ndarray

Expected Output

The correct result applying the aggregation function to the DataFrameGroupBy

Output of pd.show_versions()

INSTALLED VERSIONS

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

pandas : 1.0.0rc0
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.1
setuptools : 45.1.0
Cython : None
pytest : 5.2.0
hypothesis : None
sphinx : 2.0.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.2.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : 0.3.2
gcsfs : None
lxml.etree : None
matplotlib : 3.0.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.2.0
s3fs : 0.4.0
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.47.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExtensionArrayExtending pandas with custom dtypes or arrays.GroupbyRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions