Skip to content

BUG: df.groupby().head() missing rows when grouping on several columns with NA #45089

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
LucasG0 opened this issue Dec 28, 2021 · 1 comment · Fixed by #45102
Closed
2 of 3 tasks

BUG: df.groupby().head() missing rows when grouping on several columns with NA #45089

LucasG0 opened this issue Dec 28, 2021 · 1 comment · Fixed by #45102
Milestone

Comments

@LucasG0
Copy link
Contributor

LucasG0 commented Dec 28, 2021

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

>>> df = pd.DataFrame([["a", "z"], ["b", None], ["c", None]], columns=["X", "Y"])
>>> df
   X     Y
0  a     z
1  b  None
2  c  None
>>> df.groupby(by=["X", "Y"]).head(n=1)
   X     Y
0  a     z
1  b  None

# Other example
>>> df = pd.DataFrame([["a", "z"], ["b", None], ["c", None], ["c", None], ["c", None]], columns=["X", "Y"])
>>> df
   X     Y
0  a     z
1  b  None
2  c  None
3  c  None
4  c  None
>>> df.groupby(by=["X", "Y"]).head(n=3)
   X     Y
0  a     z
1  b  None
2  c  None
3  c  None

Issue Description

1 row having ("c", None) values is missing in both examples.

Expected Behavior

In both above examples we expect the whole dataframe to be returned. It seems the number of rows of the first group having NA in ("b", None) is deduced from the number of rows that should be returned for the second group having NA ("c", None).

Installed Versions

INSTALLED VERSIONS

commit : db08276
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-43-generic
Version : #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 1.1.3
numpy : 1.19.5
pytz : 2019.3
dateutil : 2.8.2
pip : 20.0.2
setuptools : 45.2.0
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.10.1
fastparquet : 0.7.1
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 6.0.0
pytables : None
pyxlsb : None
s3fs : 2021.10.1
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : None

@LucasG0 LucasG0 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 28, 2021
@phofl
Copy link
Member

phofl commented Dec 28, 2021

You have to set dropna=False, then this will work as expected.

This should not return rows with None at all in your examples.

@jreback jreback added this to the 1.4 milestone Dec 29, 2021
@jreback jreback added Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants