Skip to content

BUG: highlight_min and highlight_max do not work with pd.NA #42750

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
cynddl opened this issue Jul 27, 2021 · 4 comments · Fixed by #42861
Closed
2 of 3 tasks

BUG: highlight_min and highlight_max do not work with pd.NA #42750

cynddl opened this issue Jul 27, 2021 · 4 comments · Fixed by #42861
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Regression Functionality that used to work in a prior pandas version Styler conditional formatting using DataFrame.style
Milestone

Comments

@cynddl
Copy link

cynddl commented Jul 27, 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.


Code Sample, a copy-pastable example

import pandas as pd
import numpy as np

df_ok = pd.DataFrame(dict(a=[np.nan, -1, 1], b=[np.nan, -1, 1]))
df_bug = pd.DataFrame(dict(a=[pd.NA, -1, 1], b=[pd.NA, -1, 1]))

Problem description

df_ok.style.highlight_min() works correctly while df_bug.style.highlight_min() returns the following error:

TypeError: boolean value of NA is ambiguous

Fix

For highlight_min, the error comes from https://github.com/pandas-dev/pandas/blob/v1.3.1/pandas/io/formats/style.py#L2367. Specifically, from the following line:

np.where(data == np.nanmin(data.to_numpy()), props, "")

I'm assuming this could be fixed by replacing it with:

np.where(data == data.min(skipna=True), props, "")

or by making sure to_numpy converts pd.NA to np.nan?

Happy to implement a fix.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : c7f7443
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 20.4.0
Version : Darwin Kernel Version 20.4.0: Thu Apr 22 21:46:47 PDT 2021; root:xnu-7195.101.2~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_GB.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.1
numpy : 1.19.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 52.0.0.post20210125
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@cynddl cynddl added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 27, 2021
@rhshadrach rhshadrach added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Styler conditional formatting using DataFrame.style labels Jul 28, 2021
@simonjayhawkins
Copy link
Member

df_bug.style.highlight_min() was not raising TypeError: boolean value of NA is ambiguous on 1.2.5, but was not getting the expected output either. labelling as a regression for now, pending further investigation cc @attack68

@simonjayhawkins simonjayhawkins added the Regression Functionality that used to work in a prior pandas version label Jul 28, 2021
@simonjayhawkins simonjayhawkins added this to the 1.3.2 milestone Jul 28, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jul 28, 2021
@simonjayhawkins
Copy link
Member

df_bug.style.highlight_min() was not raising TypeError: boolean value of NA is ambiguous on 1.2.5

first bad commit: [0c3c56b] CLN: Styler simplify existing builtin methods (#39797)

@attack68
Copy link
Contributor

Yeh this is odd since the previous implementation before bad commit also used np.nanmin/nanmax.
@cynddl happy to receive a receive a fix with relevant tests. Also perhaps we can deal with None as well using your solution (and test for it also)

@cynddl
Copy link
Author

cynddl commented Jul 28, 2021

Sure, I'll have a look in the coming days!

@mzeitlin11 mzeitlin11 removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jul 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Regression Functionality that used to work in a prior pandas version Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants