You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
statistics.fmean() does not support Decimal data, contrary to documentation #131723
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.
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.
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')
Activity
rhettinger commentedon Mar 25, 2025
Will think about this for a while. Unlike
fsum, thesumprodfunction 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.
pjmcdermott commentedon Mar 25, 2025
Agree, my key point was that the documentation is adrift..I explicitly checked for this issue and then got hit with the error!
rhettinger commentedon Mar 25, 2025
Draft edit:
pythongh-131723: Support compatible Decimal inputs in weighted fmean