Skip to content

Groupby agg works with pd.Series.nunique, but groupby nunique fails with axis=1 #30253

Closed
@Ynob2000

Description

@Ynob2000

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({
    ('A', 'B'): [1, 2],
    ('A', 'C'): [1, 3],
    ('D', 'B'): [0, 0]
}, index=pd.bdate_range('20191212', '20191213'))

# DOESNT CRASH
df.groupby(axis=1, level=0).agg(pd.Series.nunique)

# CRASH
df.groupby(axis=1, level=0).nunique()

Problem description

The groupby nunique crashes with error "ValueError: all keys need to be the same shape", whereas the groupby agg with pd.Series.nunique does not crash

Expected Output

pd.DataFrame({
'A': [1, 2],
'D': [1, 1]
}, index=pd.bdate_range('20191212', '20191213'))

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-642.6.2.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.21.1
pytest: 4.4.1
pip: 18.1
setuptools: 40.8.0.post20190628
Cython: 0.27.3
numpy: 1.13.3
scipy: 0.19.1
pyarrow: None
xarray: 0.8.2
IPython: 5.1.0
sphinx: 1.8.5
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.4
numexpr: 2.6.5
feather: None
matplotlib: 2.1.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions