-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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: pd.options.display.float_format
did not follow left side or before decimal places format
#59876
Comments
Thanks for the report. Can you include what output you currently get and the output you expect to get. |
Thanks for the information. It appears to me you are using something akin to Juptyer's pd.options.display.float_format = '{:12.3f}'.format
# Example DataFrame
df = pd.DataFrame({
'A': [123.456, 789.1011],
'B': [2.71828, 3.14159]
})
print(df)
# A B
# 0 123.456 2.718
# 1 789.101 3.142 You can see the option is having the expected impact on printed DataFrames. It's not clear to me whether this is due to a limitation on the Jupyter (or other notebooks) side. Further investigations are welcome! |
So, I think we should close this and pass it to jupyter developer? Please give me feedback on where is the best place to bring this (is it pandas or jupyter). |
After some investigation, even string format didn't respected by display. pd.options.display.float_format = '{:.3f}'.format. # if float, use .3f
df_formatted = df.map(lambda x: str(f'{x:12.3f}')).astype('string'). # change to string to ignore float_format
display(df_formatted)
print(df_formatted) # correctly using str(f'{x:12.3f}') The workaround is to directly change Styler: styled_df = df_formatted.style.set_table_styles(
[{'selector': 'td', 'props': [('min-width', '80px')]}]
)
display(styled_df) |
Thanks for the investigation - I think your investigation suggests this is an issue with HTML formatting. We still control the HTML that is produced by Leaving this open for now. I plan to investigate it in the near future. |
Two things need to change in order to implement this. First, is passing For the 2nd, we also fix other issues with multiple spaces in strings, e.g.
fixed: main: I think each of these are not controversial, marking as a good first issue for now. But cc @pandas-dev/pandas-core for any thoughts. |
Are you expecting a pytest script to cover this change? |
Yes - I think something along the lines of |
take |
For the 2nd implementation is it ok if the html output is |
@saldanhad - offhand I'm not sure; it may be that another section of code is adding |
Thanks for clarifying. The leading |
Interesting - I'll take a deeper look. |
Meanwhile I was able to implement the |
Sure, that sounds fine. |
The issue with the extra space is the use of strip here: pandas/pandas/io/formats/html.py Line 197 in f598670
By replacing spaces with I think the solution is to leave |
Thanks for your help on this. Implementing it, this way, does give the desired outcome now. My understanding prior was to have replace come inside the if conditional before pretty print and it wasn't getting implemented. However still a few tests are failing and further investigation is needed:
|
Perhaps it's better to use |
Thanks, this worked, raising PR to close this issue. |
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
Issue Description
Pandas
pd.options.display.float_format
did not follow left side or before decimal places format.Expected Behavior
If also follows the left side or before decimal places format.
Installed Versions
INSTALLED VERSIONS
commit : d9cdd2e
python : 3.10.11.final.0
python-bits : 64
OS : Darwin
OS-release : 23.6.0
Version : Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.1.0
pip : 24.0
Cython : None
pytest : 8.1.1
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.23.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.4
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.4
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: