Skip to content

BUG: max and min returns incorrect result #39607

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
YarShev opened this issue Feb 5, 2021 · 5 comments
Closed
2 of 3 tasks

BUG: max and min returns incorrect result #39607

YarShev opened this issue Feb 5, 2021 · 5 comments
Labels
Bug Reduction Operations sum, mean, min, max, etc.

Comments

@YarShev
Copy link
Contributor

YarShev commented Feb 5, 2021

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

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

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


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import pandas
import numpy as np
df = pandas.DataFrame(
    {
        "int_col": [-5, 2, 7, 16],
        "float_col": [np.NaN, -9.4, 10.1, np.NaN],
        "str_col": ["a", np.NaN, "c", "d"],
        "bool_col": [False, True, True, False],
    }
)
df
   int_col  float_col  bool_col
0       -5        NaN     False
1        2       -9.4      True
2        7       10.1      True
3       16        NaN     False
# pandas 1.1.5 behavior:
df.max(axis=0, numeric_only=True)
int_col      16.0
float_col    10.1
bool_col      1.0
dtype: float64
# pandas 1.2.1 behavior:
df.max(axis=0, numeric_only=True)
int_col        16
float_col    10.1
bool_col     True
dtype: object

# the difference is in last two lines
bool_col      1.0
dtype: float64
# and
bool_col     True
dtype: object

Problem description

The same relates to min.

Expected Output

Output of pd.show_versions()

pandas : 1.2.1
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 52.0.0.post20210125
Cython : None
pytest : 6.2.1
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.3
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : 0.5.2
scipy : 1.6.0
sqlalchemy : 1.3.22
tables : 3.6.1
tabulate : None
xarray : 0.16.2
xlrd : 2.0.1
xlwt : None
numba : None

@YarShev YarShev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 5, 2021
@jreback
Copy link
Contributor

jreback commented Feb 5, 2021

are you sure? the prior was coerced to float

@YarShev
Copy link
Contributor Author

YarShev commented Feb 5, 2021

To me, 1.2.1 behavior looks more correct. I just wanted to make sure of this. If you think so too, the issue should be closed.

@jbrockmendel
Copy link
Member

i think this is the result of the change to operate block-wise instead of on df.values

@jorisvandenbossche
Copy link
Member

The 1.2 result preserves more information about the actual dtype of the original columns, but on the other hand it also results in a less-useful object dtype Series (and this object dtype can then propagate in following operations).

See also #39817, as this is also related to the question how to combine bool + numeric dtypes.

@jbrockmendel jbrockmendel added Reduction Operations sum, mean, min, max, etc. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
@jbrockmendel
Copy link
Member

This is correct as-is, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reduction Operations sum, mean, min, max, etc.
Projects
None yet
Development

No branches or pull requests

4 participants