Skip to content

BUG: Groupby with aggregation nunique and NaN leads to strange behavior (duplicated rows in output) #45226

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
2 of 3 tasks
LustigePerson opened this issue Jan 6, 2022 · 1 comment
Labels
Bug Duplicate Report Duplicate issue or pull request Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@LustigePerson
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {"a": [1,1,1,np.NaN],
     "b": [1,1,2,np.NaN,],
     "c": ["a", "b", "c", "d"]})

print(df.groupby(["a", "b"], dropna=False)[["c"]].agg(["unique", "nunique"]))
print(df.groupby(["a", "b"], dropna=False)[["c"]].agg(["nunique", "unique"]))

Issue Description

As you can see from he example, if I use the agg function with nunique before unique, the row with the NaNs appears duplicated:

Correct:

print(df.groupby(["a", "b"], dropna=False)[["c"]].agg(["unique", "nunique"]))

              c        
         unique nunique
a   b                  
1.0 1.0  [a, b]       2
    2.0     [c]       1
NaN NaN     [d]       1

Incorrect

print(df.groupby(["a", "b"], dropna=False)[["c"]].agg(["nunique", "unique"]))

              c        
        nunique  unique
a   b                  
1.0 1.0       2  [a, b]
    2.0       1     [c]
NaN NaN       1     [d]
    NaN       1     [d]

This only happens with NaNs, if I fillna(0) everything works as expected.

Expected Behavior

The order of aggregation functions should not impact the result and rows should not be duplicated.

Installed Versions

INSTALLED VERSIONS

commit : 66e3805
python : 3.10.1.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-147-generic
Version : #151-Ubuntu SMP Fri Jun 18 19:21:19 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.5
numpy : 1.22.0
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 60.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.30.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.5.1
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@LustigePerson LustigePerson added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 6, 2022
@phofl
Copy link
Member

phofl commented Jan 7, 2022

This is a duplicate of #42016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants