Skip to content

Regression bugs when applying GroupBy Aggregations to Categorical columns #31256

Closed
@frances-h

Description

@frances-h
>>> import pandas as pd

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

>>> df.groupby('groups').agg(pd.Series.nunique)
       value
groups
0          1
1          1
2        NaN

>>> df.groupby('groups').agg(pd.Series.nunique)['value'].dtype
CategoricalDtype(categories=[0, 1], ordered=False)

>>> df.groupby('groups').agg(pd.Series.count)
       value
groups
0          1
1        NaN
2        NaN

>>> df.groupby('groups').agg(pd.Series.mode)
                                                                           value
groups
0                                                                              0
1              0    0
Name: value, dtype: category
Categories (2, int64): [0, 1]
2       0    0
1    1
Name: value, dtype: category
Categories (2, int64): [0, 1]

Problem description

There's some regression bugs in 1.0.0rc0 when applying groupby aggregations to Categorical columns. All of these examples work as expected in 0.25.3.

Applying pd.Series.nunique and pd.Series.count both result in NaN for some groupings, the result has a Categorical dtype. The resulting dtype should be int64 and NaN should not be produced.

Applying pd.Series.mode returns the correct result for the first grouping but produces nonsensical output for subsequent groupings.

Expected Output

Aggregations should work as expected on Categorical columns as they do in 0.25.3:

>>> df.groupby('groups').agg(pd.Series.nunique)
        value
groups
0           1
1           1
2           2

>>> df.groupby('groups').agg(pd.Series.nunique)['value'].dtype
dtype('int64')

>>> df.groupby('groups').agg(pd.Series.count)
        value
groups
0           1
1           2
2           2

>>> df.groupby('groups').agg(pd.Series.mode)
         value
groups
0      0     0
1      0     0
2      0     0
       1     1

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

Labels

CategoricalCategorical Data TypeGroupbyRegressionFunctionality 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