diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 844e062b20ca3..21e75d5d1ad07 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -97,7 +97,7 @@ Other Enhancements - :func:`merge_asof` now gives a more clear error message when merge keys are categoricals that are not equal (:issue:`26136`) - :meth:`pandas.core.window.Rolling` supports exponential (or Poisson) window type (:issue:`21303`) - :class:`DatetimeIndex` and :class:`TimedeltaIndex` now have a `mean` method (:issue:`24757`) -- +- :meth:`DataFrame.describe` now formats integer percentiles without decimal point (:issue:`26660`) .. _whatsnew_0250.api_breaking: diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 765b31f294bcb..f632bc13a5b24 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1246,7 +1246,7 @@ def format_percentiles(percentiles): raise ValueError("percentiles should all be in the interval [0,1]") percentiles = 100 * percentiles - int_idx = (percentiles.astype(int) == percentiles) + int_idx = np.isclose(percentiles.astype(int), percentiles) if np.all(int_idx): out = percentiles.astype(int).astype(str) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 568b229435434..18d8d351e48c1 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -704,6 +704,17 @@ def test_describe_tz_values(self, tz_naive_fixture): result = df.describe(include='all') tm.assert_frame_equal(result, expected) + def test_describe_percentiles_integer_idx(self): + # Issue 26660 + df = pd.DataFrame({'x': [1]}) + pct = np.linspace(0, 1, 10 + 1) + result = df.describe(percentiles=pct) + + expected = DataFrame( + {'x': [1.0, 1.0, np.NaN, 1.0, *[1.0 for _ in pct], 1.0]}, + index=['count', 'mean', 'std', 'min', '0%', '10%', '20%', '30%', + '40%', '50%', '60%', '70%', '80%', '90%', '100%', 'max']) + tm.assert_frame_equal(result, expected) # --------------------------------------------------------------------- # Reductions diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index bae2470459f62..edb7c2136825d 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2740,6 +2740,14 @@ def test_format_percentiles(): fmt.format_percentiles([0.1, 0.5, 'a']) +def test_format_percentiles_integer_idx(): + # Issue #26660 + result = fmt.format_percentiles(np.linspace(0, 1, 10 + 1)) + expected = ['0%', '10%', '20%', '30%', '40%', '50%', + '60%', '70%', '80%', '90%', '100%'] + assert result == expected + + def test_repr_html_ipython_config(ip): code = textwrap.dedent("""\ import pandas as pd