mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-140938: Raise ValueError for infinite inputs to stdev/pstdev (GH-141531)
Raise ValueError for infinite inputs to stdev/pstdev --- Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
1281be1caf
commit
f0a8bc737a
3 changed files with 24 additions and 5 deletions
|
|
@ -619,9 +619,14 @@ def stdev(data, xbar=None):
|
|||
if n < 2:
|
||||
raise StatisticsError('stdev requires at least two data points')
|
||||
mss = ss / (n - 1)
|
||||
try:
|
||||
mss_numerator = mss.numerator
|
||||
mss_denominator = mss.denominator
|
||||
except AttributeError:
|
||||
raise ValueError('inf or nan encountered in data')
|
||||
if issubclass(T, Decimal):
|
||||
return _decimal_sqrt_of_frac(mss.numerator, mss.denominator)
|
||||
return _float_sqrt_of_frac(mss.numerator, mss.denominator)
|
||||
return _decimal_sqrt_of_frac(mss_numerator, mss_denominator)
|
||||
return _float_sqrt_of_frac(mss_numerator, mss_denominator)
|
||||
|
||||
|
||||
def pstdev(data, mu=None):
|
||||
|
|
@ -637,9 +642,14 @@ def pstdev(data, mu=None):
|
|||
if n < 1:
|
||||
raise StatisticsError('pstdev requires at least one data point')
|
||||
mss = ss / n
|
||||
try:
|
||||
mss_numerator = mss.numerator
|
||||
mss_denominator = mss.denominator
|
||||
except AttributeError:
|
||||
raise ValueError('inf or nan encountered in data')
|
||||
if issubclass(T, Decimal):
|
||||
return _decimal_sqrt_of_frac(mss.numerator, mss.denominator)
|
||||
return _float_sqrt_of_frac(mss.numerator, mss.denominator)
|
||||
return _decimal_sqrt_of_frac(mss_numerator, mss_denominator)
|
||||
return _float_sqrt_of_frac(mss_numerator, mss_denominator)
|
||||
|
||||
|
||||
## Statistics for relations between two inputs #############################
|
||||
|
|
|
|||
|
|
@ -2005,7 +2005,6 @@ def test_iter_list_same(self):
|
|||
expected = self.func(data)
|
||||
self.assertEqual(self.func(iter(data)), expected)
|
||||
|
||||
|
||||
class TestPVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
|
||||
# Tests for population variance.
|
||||
def setUp(self):
|
||||
|
|
@ -2113,6 +2112,14 @@ def test_center_not_at_mean(self):
|
|||
self.assertEqual(self.func(data), 2.5)
|
||||
self.assertEqual(self.func(data, mu=0.5), 6.5)
|
||||
|
||||
def test_gh_140938(self):
|
||||
# Inputs with inf/nan should raise a ValueError
|
||||
with self.assertRaises(ValueError):
|
||||
self.func([1.0, math.inf])
|
||||
with self.assertRaises(ValueError):
|
||||
self.func([1.0, math.nan])
|
||||
|
||||
|
||||
class TestSqrtHelpers(unittest.TestCase):
|
||||
|
||||
def test_integer_sqrt_of_frac_rto(self):
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
The :func:`statistics.stdev` and :func:`statistics.pstdev` functions now raise a
|
||||
:exc:`ValueError` when the input contains an infinity or a NaN.
|
||||
Loading…
Add table
Add a link
Reference in a new issue