Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ynob2000 opened this issue Dec 13, 2019 · 4 comments · Fixed by #30311
Closed

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

Ynob2000 opened this issue Dec 13, 2019 · 4 comments · Fixed by #30311
Labels

Comments

@Ynob2000
Copy link
Contributor

Ynob2000 commented Dec 13, 2019

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

@jreback
Copy link
Contributor

jreback commented Dec 13, 2019

this is. very old version of pandas pls try on latest and on master

@Ynob2000
Copy link
Contributor Author

Ynob2000 commented Dec 13, 2019

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

Traceback (most recent call last):
File "", line 1, in
File "/home/nlepleux/git/pandas-nlepleux/pandas/core/groupby/generic.py", line 942, in aggregate
result = self._aggregate_frame(func)
File "/home/nlepleux/git/pandas-nlepleux/pandas/core/groupby/generic.py", line 1123, in _aggregate_frame
fres = func(data, *args, **kwargs)
File "/home/nlepleux/git/pandas-nlepleux/pandas/core/base.py", line 1320, in nunique
uniqs = self.unique()
File "/home/nlepleux/git/pandas-nlepleux/pandas/core/generic.py", line 5201, in getattr
return object.getattribute(self, name)
AttributeError: 'DataFrame' object has no attribute 'unique'

@WillAyd
Copy link
Member

WillAyd commented Dec 17, 2019

I think an issue with axis=1 - this would work if transposing:

>>> df.T.groupby(level=0).nunique().T
            A  D
2019-12-12  1  1
2019-12-13  2  1

Interested in trying to debug @nlepleux ?

@WillAyd WillAyd changed the title Groupby agg works with pd.Series.nunique, but groupby nunique fails Groupby agg works with pd.Series.nunique, but groupby nunique fails with axis=1 Dec 17, 2019
@Ynob2000
Copy link
Contributor Author

I can surely try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants