From e20d833c27f29d86a0460ca6c2683ae8397c2de2 Mon Sep 17 00:00:00 2001 From: weikhor Date: Tue, 30 Aug 2022 02:16:21 +0800 Subject: [PATCH 1/5] add test --- pandas/io/formats/format.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 27094fff5f812..cf868560f540e 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1721,10 +1721,10 @@ def format_percentiles( percentiles = 100 * percentiles - int_idx = np.isclose(percentiles.astype(int), percentiles) + int_idx = np.isclose(percentiles.round().astype(int), 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) From 1e07daf4a0f9379d3ba3493e5cf16e421d1e8f40 Mon Sep 17 00:00:00 2001 From: weikhor Date: Tue, 30 Aug 2022 02:38:13 +0800 Subject: [PATCH 2/5] add test --- pandas/io/formats/format.py | 2 +- pandas/tests/io/formats/test_format.py | 35 ++++++++++++++++---------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index cf868560f540e..31823870706b7 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1737,7 +1737,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] diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index a7c9c86b3d9a5..827160222a2e5 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -3306,24 +3306,33 @@ def test_nat_representations(self): assert f(NaT) == "NaT" -def test_format_percentiles(): - result = fmt.format_percentiles([0.01999, 0.02001, 0.5, 0.666666, 0.9999]) - expected = ["1.999%", "2.001%", "50%", "66.667%", "99.99%"] +@pytest.mark.parametrize( + "percentiles, expected", + [ + ( + [0.01999, 0.02001, 0.5, 0.666666, 0.9999], + ["1.999%", "2.001%", "50%", "66.667%", "99.99%"], + ), + ( + [0, 0.5, 0.02001, 0.5, 0.666666, 0.9999], + ["0%", "50%", "2.0%", "50%", "66.67%", "99.99%"], + ), + ([0.281, 0.29, 0.57, 0.58], ["28.1%", "29%", "57%", "58%"]), + ], +) +def test_format_percentiles(percentiles, expected): + result = fmt.format_percentiles(percentiles) assert result == expected - result = fmt.format_percentiles([0, 0.5, 0.02001, 0.5, 0.666666, 0.9999]) - expected = ["0%", "50%", "2.0%", "50%", "66.67%", "99.99%"] - assert result == expected +@pytest.mark.parametrize( + "percentiles", + [([0.1, np.nan, 0.5]), ([-0.001, 0.1, 0.5]), ([2, 0.1, 0.5]), ([0.1, 0.5, "a"])], +) +def test_error_format_percentiles(percentiles): 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]) - with pytest.raises(ValueError, match=msg): - fmt.format_percentiles([-0.001, 0.1, 0.5]) - with pytest.raises(ValueError, match=msg): - fmt.format_percentiles([2, 0.1, 0.5]) - with pytest.raises(ValueError, match=msg): - fmt.format_percentiles([0.1, 0.5, "a"]) + fmt.format_percentiles(percentiles) def test_format_percentiles_integer_idx(): From 6e90e3a34abe7da9956f7f19dde2a76637d7d7ce Mon Sep 17 00:00:00 2001 From: weikhor Date: Wed, 31 Aug 2022 09:02:00 +0800 Subject: [PATCH 3/5] add --- doc/source/whatsnew/v1.6.0.rst | 2 +- pandas/io/formats/format.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index eac5e5d3a0f52..7011ce8a3feee 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -151,7 +151,7 @@ Interval Indexing ^^^^^^^^ -- +- Bug in :func:`format_percentiles` when formatting percentiles, more decimals than needed shown(:issue:`46362`) - Missing diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 31823870706b7..0250b2bdda709 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1720,11 +1720,12 @@ def format_percentiles( raise ValueError("percentiles should all be in the interval [0,1]") percentiles = 100 * percentiles + percentiles_round_type = percentiles.round().astype(int) - int_idx = np.isclose(percentiles.round().astype(int), percentiles) + int_idx = np.isclose(percentiles_round_type, percentiles) if np.all(int_idx): - out = percentiles.round().astype(int).astype(str) + out = percentiles_round_type.astype(str) return [i + "%" for i in out] unique_pcts = np.unique(percentiles) From 62fd28eda4501dbafba9d59fb0ee6c0ddb29c854 Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Thu, 1 Sep 2022 02:05:03 +0800 Subject: [PATCH 4/5] Update doc/source/whatsnew/v1.6.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v1.6.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index 2849f1554c812..86620cd26436c 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -151,7 +151,7 @@ Interval Indexing ^^^^^^^^ -- Bug in :func:`format_percentiles` when formatting percentiles, more decimals than needed shown(:issue:`46362`) +- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`) - Missing From f2479e8e8f57e4f4b631407754c9d4fafa3bee3d Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 3 Sep 2022 18:36:21 +0800 Subject: [PATCH 5/5] add --- pandas/tests/io/formats/test_format.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 827160222a2e5..443aa743a6444 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -3318,6 +3318,7 @@ def test_nat_representations(self): ["0%", "50%", "2.0%", "50%", "66.67%", "99.99%"], ), ([0.281, 0.29, 0.57, 0.58], ["28.1%", "29%", "57%", "58%"]), + ([0.28, 0.29, 0.57, 0.58], ["28%", "29%", "57%", "58%"]), ], ) def test_format_percentiles(percentiles, expected):