Skip to content

Added the round( ) function to avoid error while multiplication of float numbers issue: #46362 #46388

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Bug fixes
~~~~~~~~~
- Fix some cases for subclasses that define their ``_constructor`` properties as general callables (:issue:`46018`)
- Fixed "longtable" formatting in :meth:`.Styler.to_latex` when ``column_format`` is given in extended format (:issue:`46037`)
-

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this please?


.. ---------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ Conversion
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` from floating dtype to unsigned integer dtype failing to raise in the presence of negative values (:issue:`45151`)
- Bug in :func:`array` with ``FloatingDtype`` and values containing float-castable strings incorrectly raising (:issue:`45424`)
- Bug when comparing string and datetime64ns objects causing ``OverflowError`` exception. (:issue:`45506`)
- Bug in :meth:`format.py` the percentiles values are converted to integer dtype, which ignores the floating point error while multiplying. (:issue:`46362`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit confusing. What about something more like "Bug when formatting percentiles, more decimals than needed shown" or something similar.


Strings
^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,10 +1713,10 @@ def format_percentiles(

percentiles = 100 * percentiles

int_idx = np.isclose(percentiles.astype(int), percentiles)
int_idx = np.isclose(percentiles.round(), percentiles)

if np.all(int_idx):
out = percentiles.astype(int).astype(str)
out = percentiles.round().astype(int).astype(str)
return [i + "%" for i in out]

unique_pcts = np.unique(percentiles)
Expand All @@ -1729,7 +1729,7 @@ def format_percentiles(
).astype(int)
prec = max(1, prec)
out = np.empty_like(percentiles, dtype=object)
out[int_idx] = percentiles[int_idx].astype(int).astype(str)
out[int_idx] = percentiles[int_idx].round().astype(int).astype(str)

out[~int_idx] = percentiles[~int_idx].round(prec).astype(str)
return [i + "%" for i in out]
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3262,6 +3262,11 @@ def test_format_percentiles():
expected = ["0%", "50%", "2.0%", "50%", "66.67%", "99.99%"]
assert result == expected

# Issue #46362
result = fmt.format_percentiles([0.281,0.57,0.58,0.29])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need spaces after the commas. Also, those are supposed to be percentiles, so they should be sorted.

expected = ['28.1%', '57%', '58%', '29%']
assert result == expected

msg = r"percentiles should all be in the interval \[0,1\]"
with pytest.raises(ValueError, match=msg):
fmt.format_percentiles([0.1, np.nan, 0.5])
Expand Down