The *data* can be any iterable containing sample data. For meaningful
results, the number of data points in *data* should be larger than *n*.
- Raises :exc:`StatisticsError` if there are not at least two data points.
+ Raises :exc:`StatisticsError` if there is not at least one data point.
The cut points are linearly interpolated from the
two nearest data points. For example, if a cut point falls one-third
.. versionadded:: 3.8
+ .. versionchanged:: 3.13
+ No longer raises an exception for an input with only a single data point.
+ This allows quantile estimates to be built up one sample point
+ at a time becoming gradually more refined with each new data point.
+
.. function:: covariance(x, y, /)
Return the sample covariance of two inputs *x* and *y*. Covariance
data = random.choices(range(100), k=k)
q1, q2, q3 = quantiles(data, method='inclusive')
self.assertEqual(q2, statistics.median(data))
+ # Base case with a single data point: When estimating quantiles from
+ # a sample, we want to be able to add one sample point at a time,
+ # getting increasingly better estimates.
+ self.assertEqual(quantiles([10], n=4), [10.0, 10.0, 10.0])
+ self.assertEqual(quantiles([10], n=4, method='exclusive'), [10.0, 10.0, 10.0])
def test_equal_inputs(self):
quantiles = statistics.quantiles
with self.assertRaises(ValueError):
quantiles([10, 20, 30], method='X') # method is unknown
with self.assertRaises(StatisticsError):
- quantiles([10], n=4) # not enough data points
+ quantiles([], n=4) # not enough data points
with self.assertRaises(TypeError):
quantiles([10, None, 30], n=4) # data is non-numeric