Skip to content

Add zero value support for statistics.geometric_mean() #112540

Closed
@milthorpe

Description

@milthorpe

Bug report

Bug description:

The implementation of statistics.geometric_mean using logarithms requires that all input values must be positive. However, a real geometric mean is defined for all sets of non-negative real values. The geo mean of any set of numbers containing zero is itself zero.

from statistics import geometric_mean
geometric_mean([1.0, 2.0, 0.0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/milthorpe/miniconda3/lib/python3.11/statistics.py", line 489, in geometric_mean
    raise StatisticsError('geometric mean requires a non-empty dataset '
statistics.StatisticsError: geometric mean requires a non-empty dataset containing positive numbers

I believe geometric_mean should return 0 if any of the input values are zero. (It should continue to return a StatisticsError if any of the input values are negative.)

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions