Skip to content
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

BUG: df.nsmallest get wrong results when row contains NaN #46589

Closed
2 of 3 tasks
Yikun opened this issue Apr 1, 2022 · 1 comment · Fixed by #46656
Closed
2 of 3 tasks

BUG: df.nsmallest get wrong results when row contains NaN #46589

Yikun opened this issue Apr 1, 2022 · 1 comment · Fixed by #46656
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@Yikun
Copy link
Contributor

Yikun commented Apr 1, 2022

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 main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd
pdf = pd.DataFrame(
    {"a": [1, 2, 3, 4, 5, None, 7], "b": [7, 6, 5, 4, 3, 2, 1], "c": [1, 1, 2, 2, 3, 3, 3]},
    index=np.random.rand(7),
)

Issue Description

Before pandas 1.4.0(such as 1.3.5), it is right behavior

>>> pdf.nsmallest(5, columns=["a", "b"])
            a  b  c
0.527613  1.0  7  1
0.448691  2.0  6  1
0.782117  3.0  5  2
0.524464  4.0  4  2
0.895850  5.0  3  3

Since pandas 1.4.0 (1.4.0/1.4.1)

>>> pdf.nsmallest(5, columns=["a", "b"])
            a  b  c
0.527613  1.0  7  1
0.448691  2.0  6  1
0.782117  3.0  5  2
0.524464  4.0  4  2
0.895850  5.0  3  3
0.239389  NaN  2  3

Expected Behavior

>>> pdf.nsmallest(5, columns=["a", "b"])
            a  b  c
0.527613  1.0  7  1
0.448691  2.0  6  1
0.782117  3.0  5  2
0.524464  4.0  4  2
0.895850  5.0  3  3

get right results since 1.4.0.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-58-generic
Version : #64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.1
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 44.0.0
Cython : None
pytest : 7.1.1
hypothesis : None
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.2.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.2.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 3.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : 1.4.34
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

@Yikun Yikun added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 1, 2022
@Yikun Yikun changed the title BUG: BUG: df.nsmallest get wrong results when row contains NaN Apr 1, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Apr 5, 2022
@simonjayhawkins simonjayhawkins added this to the 1.4.3 milestone Apr 5, 2022
@simonjayhawkins
Copy link
Member

Thanks @Yikun for the report.

Before pandas 1.4.0(such as 1.3.5), it is right behavior

yes, it appears that the result should be the same as df.sort_values(["a", "b"], ascending=True, na_position="last").head(5)

first bad commit: [16d2f59] BUG: nlargest/nsmallest can now consider nan values like sort_values(ascending=True).head(n) (#43060)

PR to fix welcome.

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version Numeric Operations Arithmetic, Comparison, and Logical operations and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants