Skip to content

Commit 385336f

Browse files
committed
Final-commit
1 parent f3bfe4e commit 385336f

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

pandas/core/methods/describe.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,13 @@ def describe_numeric_1d(series: Series, percentiles: Sequence[float]) -> Series:
227227
"""
228228
from pandas import Series
229229

230+
# Fix for issue #60550 :
231+
# if percentiles != []:
230232
formatted_percentiles = format_percentiles(percentiles)
231233

234+
# else:
235+
# formatted_percentiles = []
236+
232237
stat_index = ["count", "mean", "std", "min"] + formatted_percentiles + ["max"]
233238
d = (
234239
[series.count(), series.mean(), series.std(), series.min()]
@@ -345,14 +350,21 @@ def _refine_percentiles(
345350
percentiles : list-like of numbers, optional
346351
The percentiles to include in the output.
347352
"""
353+
# Fix for issue #60550 :
354+
from pandas import Series
355+
348356
if percentiles is None:
349357
return np.array([0.25, 0.5, 0.75])
350358

359+
# Fix for issue #60550 :
360+
elif isinstance(percentiles, (list, np.ndarray, Series)) and len(percentiles) == 0:
361+
return np.array([])
362+
351363
# explicit conversion of `percentiles` to list
352364
percentiles = list(percentiles)
353365

354366
# get them all to be in [0, 1]
355-
validate_percentile(percentiles)
367+
validate_percentile(percentiles)
356368

357369
percentiles = np.asarray(percentiles)
358370

pandas/io/formats/format.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,7 @@ def format_percentiles(
15651565
>>> format_percentiles([0, 0.5, 0.02001, 0.5, 0.666666, 0.9999])
15661566
['0%', '50%', '2.0%', '50%', '66.67%', '99.99%']
15671567
"""
1568+
15681569
percentiles = np.asarray(percentiles)
15691570

15701571
# It checks for np.nan as well
@@ -1575,7 +1576,9 @@ def format_percentiles(
15751576
):
15761577
raise ValueError("percentiles should all be in the interval [0,1]")
15771578

1578-
percentiles = 100 * percentiles
1579+
# Fix for issue #60550
1580+
percentiles = 100 * percentiles if percentiles else np.array([])
1581+
15791582
prec = get_precision(percentiles)
15801583
percentiles_round_type = percentiles.round(prec).astype(int)
15811584

@@ -1595,6 +1598,10 @@ def format_percentiles(
15951598

15961599

15971600
def get_precision(array: np.ndarray | Sequence[float]) -> int:
1601+
# Fix for issue #60550
1602+
if array.size == 0:
1603+
return 0
1604+
15981605
to_begin = array[0] if array[0] > 0 else None
15991606
to_end = 100 - array[-1] if array[-1] < 100 else None
16001607
diff = np.ediff1d(array, to_begin=to_begin, to_end=to_end)

pandas/tests/frame/methods/test_describe.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,32 @@ def test_describe_exclude_pa_dtype(self):
413413
dtype=pd.ArrowDtype(pa.float64()),
414414
)
415415
tm.assert_frame_equal(result, expected)
416+
417+
def test_describe_empty_percentiles(self):
418+
# 60550 :
419+
# Create a simple DataFrame
420+
df = DataFrame({"a": [1, 2, 3, 4, 5]})
421+
422+
# Case 1: Passing an empty list
423+
result = df.describe(percentiles=[])
424+
expected = DataFrame(
425+
{"a": [5, 3, 1, 5]},
426+
index=["count", "mean", "min", "max"],
427+
)
428+
tm.assert_frame_equal(result, expected)
429+
430+
# Case 2: Passing an empty numpy array
431+
result = df.describe(percentiles=np.array([]))
432+
tm.assert_frame_equal(result, expected)
433+
434+
def test_describe_with_single_percentile(self):
435+
# 60550 :
436+
# Create a simple DataFrame
437+
df = DataFrame({"a": [1, 2, 3, 4, 5]})
438+
# Case 1: Passing a single percentile
439+
result = df.describe(percentiles=[0.5])
440+
expected = DataFrame(
441+
{"a": [5, 3, 1, 3.0]},
442+
index=["count", "mean", "min", "50%"],
443+
)
444+
tm.assert_frame_equal(result, expected)

test_self.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy as np
2+
3+
import pandas as pd
4+
5+
# creating a single series dataframe
6+
frame = pd.DataFrame(np.array([1, 2, 3, 4, 5, 100]))
7+
8+
# getting the describe with single percentile value
9+
print(frame.describe(percentiles=[0.25]))

0 commit comments

Comments
 (0)