Skip to content

Commit 62405c7

Browse files
authored
gh-110150: Fix base case handling in quantiles() (gh-110151)
1 parent a46e960 commit 62405c7

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

Doc/library/statistics.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ However, for reading convenience, most of the examples show sorted sequences.
585585

586586
The *data* can be any iterable containing sample data. For meaningful
587587
results, the number of data points in *data* should be larger than *n*.
588-
Raises :exc:`StatisticsError` if there are not at least two data points.
588+
Raises :exc:`StatisticsError` if there is not at least one data point.
589589

590590
The cut points are linearly interpolated from the
591591
two nearest data points. For example, if a cut point falls one-third
@@ -625,6 +625,11 @@ However, for reading convenience, most of the examples show sorted sequences.
625625

626626
.. versionadded:: 3.8
627627

628+
.. versionchanged:: 3.13
629+
No longer raises an exception for an input with only a single data point.
630+
This allows quantile estimates to be built up one sample point
631+
at a time becoming gradually more refined with each new data point.
632+
628633
.. function:: covariance(x, y, /)
629634

630635
Return the sample covariance of two inputs *x* and *y*. Covariance

Lib/statistics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,9 @@ def quantiles(data, *, n=4, method='exclusive'):
844844
data = sorted(data)
845845
ld = len(data)
846846
if ld < 2:
847-
raise StatisticsError('must have at least two data points')
847+
if ld == 1:
848+
return data * (n - 1)
849+
raise StatisticsError('must have at least one data point')
848850
if method == 'inclusive':
849851
m = ld - 1
850852
result = []

Lib/test/test_statistics.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,11 @@ def f(x):
24542454
data = random.choices(range(100), k=k)
24552455
q1, q2, q3 = quantiles(data, method='inclusive')
24562456
self.assertEqual(q2, statistics.median(data))
2457+
# Base case with a single data point: When estimating quantiles from
2458+
# a sample, we want to be able to add one sample point at a time,
2459+
# getting increasingly better estimates.
2460+
self.assertEqual(quantiles([10], n=4), [10.0, 10.0, 10.0])
2461+
self.assertEqual(quantiles([10], n=4, method='exclusive'), [10.0, 10.0, 10.0])
24572462

24582463
def test_equal_inputs(self):
24592464
quantiles = statistics.quantiles
@@ -2504,7 +2509,7 @@ def test_error_cases(self):
25042509
with self.assertRaises(ValueError):
25052510
quantiles([10, 20, 30], method='X') # method is unknown
25062511
with self.assertRaises(StatisticsError):
2507-
quantiles([10], n=4) # not enough data points
2512+
quantiles([], n=4) # not enough data points
25082513
with self.assertRaises(TypeError):
25092514
quantiles([10, None, 30], n=4) # data is non-numeric
25102515

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix base case handling in statistics.quantiles. Now allows a single data
2+
point.

0 commit comments

Comments
 (0)