Skip to content

statistics.fmean() does not support Decimal data, contrary to documentation #131723

Open

Description

@pjmcdermott

Bug report

Bug description:

From manpage:
"Unless explicitly noted, these functions support int, float, Decimal and Fraction." and (for fmean)
"Convert data to floats and compute the arithmetic mean."

However:

Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import statistics
>>> grades = [85, 92, 83, 91]
>>> weights = [0.20, 0.20, 0.30, 0.30]
>>> statistics.fmean(grades, weights)
87.6
>>> from decimal import Decimal
>>> grades = [Decimal("85"), Decimal("92"), Decimal("83"), Decimal("91")]
>>> statistics.fmean(grades, weights)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/statistics.py", line 517, in fmean
    num = sumprod(data, weights)
          ^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'

Contrary to documentation, statistics.fmean() does not convert data to floats.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. rhettinger commented on Mar 25, 2025

    @rhettinger
    Contributor

    Will think about this for a while. Unlike fsum, the sumprod function won't autoconvert its inputs to floats. We could handle the TypeError and force all the weights to floats, but I'm not sure that is the right thing to do. Decimals don't multiply with floats for a reason. We may just document that the data is converted to floats, not the weights.

    For now, I'll mark this as a Docs issue but may yet switch it to Type-Bug or Type-Feature if the behavior needs to change.

  2. self-assigned this
    on Mar 25, 2025
  3. pjmcdermott commented on Mar 25, 2025

    @pjmcdermott
    Author

    Agree, my key point was that the documentation is adrift..I explicitly checked for this issue and then got hit with the error!

  4. rhettinger commented on Mar 25, 2025

    @rhettinger
    Contributor

    Draft edit:

    diff --git a/Lib/statistics.py b/Lib/statistics.py
    index e2b59267f04..2daa3c4cdf9 100644
    --- a/Lib/statistics.py
    +++ b/Lib/statistics.py
    @@ -187,6 +187,9 @@ def fmean(data, weights=None):
         >>> fmean([3.5, 4.0, 5.25])
         4.25
     
    +    If provided, the number of weights must match the number of data
    +    elements.  Also the data types of the weights and the data must be
    +    compatible. Decimals don't mix with floats or fractions.
         """
         if weights is None:
     
    @@ -209,7 +212,7 @@ def fmean(data, weights=None):
             weights = list(weights)
     
         try:
    -        num = sumprod(data, weights)
    +        num = float(sumprod(data, weights))
         except ValueError:
             raise StatisticsError('data and weights must be the same length')
  5. added a commit that references this issue on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docsDocumentation in the Doc dir

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions