BUG: df.nsmallest get wrong results when row contains NaN
· Issue #46589 · pandas-dev/pandas (original) (raw)
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