Skip to content

BUG: pandas.DataFrame.apply generates unexpected behavior when the np.nan is the first value in a column #54182

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
3 tasks done
jschubnell opened this issue Jul 18, 2023 · 4 comments
Labels
Apply Apply, Aggregate, Transform, Map Bug Closing Candidate May be closeable, needs more eyeballs

Comments

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

Reproducible Example

import pandas as pd
import numpy as np

data_1 = {"a": [1, 2, 3, 4],
          "b": [np.nan, -0.475, 13.795, -574]}

df_1 = pd.DataFrame(data=data_1)

result_1 = df_1.apply(min, axis=0)

print(result_1)

data_2 = {"a": [2, 1, 3, 4],
          "b": [-0.475, np.nan, 13.795, -574]}

df_2 = pd.DataFrame(data=data_2)

result_2 = df_2.apply(min, axis=0)

print(result_2)

Issue Description

Hello,

I am using the Python built-in function min() to find the smallest item in each column of my dataframe. The issue I am facing is that whenever the first value in the column b is np.nan the result is that np.nan is the smallest value when it should be -574.0. The unexpected behavior happens when I change the order of the rows, like df_2 and it returns the correct -574.0 value.

Expected Behavior

I would expect that the output for the min() value in column b is -574.0 regardless of whether the first value in the column is np.nan.
If I replace the min() for np.nanmin() I get the expected value in both cases

import pandas as pd
import numpy as np

data_1 = {"a": [1, 2, 3, 4],
         "b": [np.nan, -0.475, 13.795, -574]}

df_1 = pd.DataFrame(data=data_1)

result_1 = df_1.apply(np.nanmin, axis=0)

print(result_1)

Output:

a     1
b   -574.0
dtype: float64
data_2 = {"a": [2, 1, 3, 4],
          "b": [-0.475, np.nan, 13.795, -574]}

df_2 = pd.DataFrame(data=data_2)

result_2 = df_2.apply(np.nanmin, axis=0)

print(result_2)

Output:

a    1
b   -574.0
dtype: float64

Installed Versions

INSTALLED VERSIONS

commit : 0f43794
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-46-generic
Version : #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 21 15:35:31 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : pt_BR.UTF-8

pandas : 2.0.3
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@jschubnell jschubnell added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 18, 2023
@harikirank
Copy link

There seems to be a problem with the inbuilt min function itself. I have seen that, internally it is taking the column values and calling the inbuilt python min function. I would like to work on this issue if anyone is willing to guide me.
Thanks

@jschubnell
Copy link
Author

Thanks for the feedback @harikirank! I would like to work on this issue too if possible.

@rhshadrach
Copy link
Member

Related: #53425

I have seen that, internally it is taking the column values and calling the inbuilt python min function.

This is correct - apply takes a callable and uses it. This is what Python's min function does:

print(min([np.nan, -574.0]))
# nan
print(min([-574.0, np.nan]))
# -574.0

I think this is what should be expected by passing min to apply - you get exactly the behavior of Python's min builtin.

On main, you get -574.0 by passing "min" (the string alias). I think you should be passing this instead.

@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 18, 2023
@jschubnell
Copy link
Author

jschubnell commented Jul 18, 2023

Thank you for the response @rhshadrach! I will close this issue as indeed the apparent inconsistence comes from the built-in function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Closing Candidate May be closeable, needs more eyeballs
Projects
None yet
Development

No branches or pull requests

3 participants