Skip to content

DataFrame.groupby(grp, axis=1) with categorical grp breaks #13420

Closed
@thorbjornwolf

Description

@thorbjornwolf

While attempting to use pd.qcut (which returned a Categorical) to bin some data in groups for plotting, I encountered the following error. The idea is to group a DataFrame by columns (axis=1) using a Categorical.

Minimal breaking example

>>> import pandas
>>> df = pandas.DataFrame({'a':[1,2,3,4], 'b':[-1,-2,-3,-4], 'c':[5,6,7,8]})
>>> df
   a  b  c
0  1 -1  5
1  2 -2  6
2  3 -3  7
3  4 -4  8
>>> grp = pandas.Categorical([1,0,1])
>>> df.groupby(grp, axis=1).mean()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ntawolf/anaconda3/lib/python3.5/site-packages/pandas/core/generic.py", line 3778, in groupby
    **kwargs)
  File "/home/ntawolf/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 1427, in groupby
    return klass(obj, by, **kwds)
  File "/home/ntawolf/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 354, in __init__
    mutated=self.mutated)
  File "/home/ntawolf/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 2390, in _get_grouper
    raise ValueError("Categorical dtype grouper must "
ValueError: Categorical dtype grouper must have len(grouper) == len(data)

Expected behaviour

Same as

>>> df.T.groupby(grp, axis=0).mean().T
   0  1
0 -1  3
1 -2  4
2 -3  5
3 -4  6

So, it works as expected when doubly transposed. This makes it appear as a bug to me.

Proposed solution

In if is_categorical_dtype(gpr) and len(gpr) != len(obj):, change len(obj) to obj.shape[axis]. This assumes that len(obj) == obj.shape[0] for all obj.

So, supposing you agree that this is a bug, should a test be put in test_groupby_categorical?

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.19.0-59-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 22.0.5
Cython: 0.24
numpy: 1.10.4
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: 1.4.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.1
xlsxwriter: 0.8.9
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions