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: style.apply applies the style in subset, but it also reset the style elsewhere. #57470

Closed
2 of 3 tasks
xgnata opened this issue Feb 17, 2024 · 9 comments
Closed
2 of 3 tasks
Labels
Bug Styler conditional formatting using DataFrame.style

Comments

@xgnata
Copy link

xgnata commented Feb 17, 2024

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 
def highlight_max(x, color):
    return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};", None)
df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"])
df.style.apply(highlight_max, color='green', subset="A")
df.style.apply(highlight_max, color='red', subset="B")

Issue Description

style.apply has a "subset" parameter. The documentation is not very explicit, but one expect that style.apply applies the style on the specified subset leaving the other cells style unchanged. This is not the case. style.apply applies the style on the specified subset, but it also reset the style elsewhere in the dataframe.

Expected Behavior

One value in green in column A and one value in red in column B (in a jupyter notebook)
It should be possible to apply a style on a subset, and then another style on another subset without losing the first styling.

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.11.6.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.0-17-generic
Version : #17-Ubuntu SMP PREEMPT_DYNAMIC Thu Jan 11 14:01:59 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 68.1.2
pip : 23.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 : 3.1.3
IPython : 8.21.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@xgnata xgnata added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 17, 2024
@rhshadrach
Copy link
Member

Thanks for the report, confirmed on main. Further investigations and PRs to fix are welcome.

cc @attack68

@rhshadrach rhshadrach added Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 17, 2024
@Samyaktg
Copy link

Hello I would be glad to work on it and fix it,
Solution would be to add the inplace=True parameter so that we can easily use it in jupyter.
Would be glad if anyone can also suggest other possible solutions

@Samyaktg
Copy link

take

@Delengowski
Copy link
Contributor

This isn't a bug at all.

Every time you access pandas.DataFrame.style it produces a new Styler object. You take the output of your first access and use that from there.

@attack68

@Delengowski
Copy link
Contributor

The documentation clearly says returns a Styler object.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.style.html

@Samyaktg
Copy link

I am in the process of adding the in-place parameter to this, whilst it may not be a bug to be fixed, surely this could work as an additional feature?
(I am a beginner so would be glad to receive any advice)

@xgnata
Copy link
Author

xgnata commented Feb 17, 2024

@Delengowski : You are right.
I was confused by this page in the documentation
https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.apply.html
OK, it clearly says that it returns a Styler, but maybe one example like that could be added:

def highlight_max(x, color):
return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};", None)
df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"])
tmp = df.style.apply(highlight_max, color='green', subset="A")
tmp.apply(highlight_max, color='red', subset="B")

I'm still a bit confused by the API. I know it won't be changed, but it is not unnatural to think that df.style.apply should modify the style property of the dataframe.

@attack68
Copy link
Contributor

There is an entire guide for the Styler. Its design has been carefully put together and results in a very advanced manipulator that is decoupled from a DataFrame. What might seem unnatural now will seem natural, and more powerful later.

@Samyaktg Samyaktg removed their assignment Feb 17, 2024
@rhshadrach
Copy link
Member

Thanks! I missed that the methods were not being chained. 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

5 participants