Skip to content

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

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy