Skip to content

statistics.stdev() raises AttributeError when data contains infinity #140938

@T90REAL

Description

@T90REAL

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

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions