Skip to content

kde and kde_random functions in statistics.py accept invalid arguments #149949

@manoj-km24

Description

@manoj-km24

Bug report

Bug description:

In Lib/statistics.py, The kde() and kde_random() functions only validate the first element in the data argument passed. It accepts the following:

>>> import statistics
>>> data = [1.5, "bad", 2.4]
>>> f = statistics.kde(data,h=0.5)
>>> f
<function kde.<locals>.pdf at 0x000001905213A3E0>
>>> print(f(1))
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    print(f(1))
  File "E:\Python\Lib\statistics.py", line 980, in pdf
    return sum(K((x - x_i) / h) for x_i in data) / (n * h)
  File "E:\Python\Lib\statistics.py", line 980, in <genexpr>
    return sum(K((x - x_i) / h) for x_i in data) / (n * h)
TypeError: unsupported operand type(s) for -: 'int' and 'str'

The code in the module , only validates the first data element:

if not isinstance(data[0], (int, float)):
        raise TypeError('Data sequence must contain ints or floats')

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibStandard 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