- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 33.3k
 
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
statistics.stdev() crashes when processing data containing inf, while statistics.variance() handles the same input correctly. This violates the invariant stdev = sqrt(variance).
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'
Expected: stdev(data) should return inf (since sqrt(variance) = sqrt(inf) = inf)
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error