Skip to content

Commit 16e8ea8

Browse files
jkovacevicjreback
authored andcommitted
[26660] compare int with double by subtracting during describe percen… (#26768)
1 parent f4b6e92 commit 16e8ea8

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Other Enhancements
9797
- :func:`merge_asof` now gives a more clear error message when merge keys are categoricals that are not equal (:issue:`26136`)
9898
- :meth:`pandas.core.window.Rolling` supports exponential (or Poisson) window type (:issue:`21303`)
9999
- :class:`DatetimeIndex` and :class:`TimedeltaIndex` now have a `mean` method (:issue:`24757`)
100-
-
100+
- :meth:`DataFrame.describe` now formats integer percentiles without decimal point (:issue:`26660`)
101101

102102
.. _whatsnew_0250.api_breaking:
103103

pandas/io/formats/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ def format_percentiles(percentiles):
12461246
raise ValueError("percentiles should all be in the interval [0,1]")
12471247

12481248
percentiles = 100 * percentiles
1249-
int_idx = (percentiles.astype(int) == percentiles)
1249+
int_idx = np.isclose(percentiles.astype(int), percentiles)
12501250

12511251
if np.all(int_idx):
12521252
out = percentiles.astype(int).astype(str)

pandas/tests/frame/test_analytics.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,17 @@ def test_describe_tz_values(self, tz_naive_fixture):
704704
result = df.describe(include='all')
705705
tm.assert_frame_equal(result, expected)
706706

707+
def test_describe_percentiles_integer_idx(self):
708+
# Issue 26660
709+
df = pd.DataFrame({'x': [1]})
710+
pct = np.linspace(0, 1, 10 + 1)
711+
result = df.describe(percentiles=pct)
712+
713+
expected = DataFrame(
714+
{'x': [1.0, 1.0, np.NaN, 1.0, *[1.0 for _ in pct], 1.0]},
715+
index=['count', 'mean', 'std', 'min', '0%', '10%', '20%', '30%',
716+
'40%', '50%', '60%', '70%', '80%', '90%', '100%', 'max'])
717+
tm.assert_frame_equal(result, expected)
707718
# ---------------------------------------------------------------------
708719
# Reductions
709720

pandas/tests/io/formats/test_format.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,6 +2740,14 @@ def test_format_percentiles():
27402740
fmt.format_percentiles([0.1, 0.5, 'a'])
27412741

27422742

2743+
def test_format_percentiles_integer_idx():
2744+
# Issue #26660
2745+
result = fmt.format_percentiles(np.linspace(0, 1, 10 + 1))
2746+
expected = ['0%', '10%', '20%', '30%', '40%', '50%',
2747+
'60%', '70%', '80%', '90%', '100%']
2748+
assert result == expected
2749+
2750+
27432751
def test_repr_html_ipython_config(ip):
27442752
code = textwrap.dedent("""\
27452753
import pandas as pd

0 commit comments

Comments
 (0)