Skip to content

BUG: pandas change in style overrides defaults format for other columns #61269

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
lcrmorin opened this issue Apr 11, 2025 · 4 comments
Closed
2 of 3 tasks
Labels
Closing Candidate May be closeable, needs more eyeballs Styler conditional formatting using DataFrame.style

Comments

@lcrmorin
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
 
pd.set_option('display.float_format', '{:.2f}'.format)
 
pd.DataFrame(15.22345676543234567,columns=[1,2,3,4,5,6],index=['A','Z','R','T'])#.style.format({1:'{:.2%}'})
 
pd.DataFrame(15.22345676543234567,columns=[1,2,3,4,5,6],index=['A','Z','R','T']).style.format({1:'{:.2%}'})

Issue Description

After setting float default number of decimal place to display to 2, the first exemple works, only showing 2 decimals for all column. However when adding a custom style to only the first columns all other columns are formatted with the default 6 decimals places. This is quite counterproductive as it means if we want to set some format we need to define all format. It would be nice to be able to change only a few formats while the unchanged are set to default.

Expected Behavior

When specifying a format for a given columns, pandas should style use user specified default value for other columns.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : fr_FR.cp1252

pandas : 2.2.3
numpy : 2.2.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : 8.31.0
adbc-driver-postgresql: None
...
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@lcrmorin lcrmorin added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 11, 2025
@arthurlw
Copy link
Contributor

Confirmed this still happens on the latest main. Applying .style.format() to just one column causes the others to fall back to full precision. I’m guessing .style.format() overrides display.float_format entirely, so other columns fall back to default formatting.

@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 Apr 13, 2025
@attack68
Copy link
Contributor

Please read the notes section of https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.format.html

This is behaving as intended and as described.

Displaying a Dataframe using the Dataframe printer is entirely different to displaying a Styler using the Styler's output methods.

What you should do is this:

import pandas as pd
pd.set_option('styler.format.precision', 3)
pd.DataFrame(15.22345676543234567,columns=[1,2,3,4,5,6],index=['A','Z','R','T']).style.format({1:'{:.2%}'})

@attack68 attack68 added Closing Candidate May be closeable, needs more eyeballs and removed Bug labels Apr 13, 2025
@arthurlw
Copy link
Contributor

Thanks for the clarification! I missed that Styler uses its own formatting logic via styler.format.precision instead of display.float_format. Appreciate the doc pointer!

@lcrmorin
Copy link
Author

Indeed, behaviour is quite confusing. But I can confirm the proposed code works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

4 participants