import statistics
data = [1.0, 2.0, float('inf'), 4.0, 5.0]
# Works fine
print(statistics.variance(data)) # Returns: inf
# Crashes
statistics.stdev(data) # AttributeError: 'float' object has no attribute 'numerator'
inf
Traceback (most recent call last):
File "/data/src/test.py", line 9, in <module>
statistics.stdev(data) # AttributeError: 'float' object has no attribute 'numerator'
^^^^^^^^^^^^^^^^^^^^^^
File "/home/py312/lib/python3.12/statistics.py", line 974, in stdev
return _float_sqrt_of_frac(mss.numerator, mss.denominator)
^^^^^^^^^^^^^
AttributeError: 'float' object has no attribute 'numerator'
Bug report
Bug description:
statistics.stdev()crashes when processing data containinginf, whilestatistics.variance()handles the same input correctly. This violates the invariantstdev = sqrt(variance).Expected:
stdev(data)should returninf(sincesqrt(variance) = sqrt(inf) = inf)CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs