statistics: mean() Can't Mix Decimal With Other Types (tested)
statistics.mean() is documented as working across int, float, Decimal, and Fraction data. We measured what happens when a single call mixes two of those types together in the same list, and found it depends heavily on which two types -- some combinations work transparently, others raise a distinctive error that doesn't look like Python's usual arithmetic TypeError.
1. Mixing Fraction and float works fine and returns float
import statistics as st
from fractions import Fraction
result = st.mean([Fraction(1, 2), 0.5])
print(result, type(result))
0.5 <class 'float'>
Measured: statistics.mean() has its own internal type-coercion logic, separate from Python's normal operator dispatch -- it successfully mixes Fraction and float values in one call, promoting the result to float.
2. Mixing Decimal with float or Fraction raises TypeError with an unusual message
import statistics as st
from fractions import Fraction
from decimal import Decimal
try:
st.mean([1.5, Decimal("0.5")])
except TypeError as e:
print("TypeError:", e)
try:
st.mean([Fraction(1, 2), Decimal("0.5")])
except TypeError as e:
print("TypeError:", e)
TypeError: don't know how to coerce float and Decimal
TypeError: don't know how to coerce Decimal and Fraction
Measured: both combinations raise TypeError, but with the message "don't know how to coerce <type> and <type>" -- this comes from statistics's own internal _coerce() helper, not from Python's normal +// operators, so it reads differently from the TypeError you'd get calling 1.5 + Decimal("0.5") directly ("unsupported operand type(s) for +"). Grepping logs for the standard arithmetic error text won't catch this one.
3. Decimal only coerces cleanly with int, not with float or Fraction
import statistics as st
from decimal import Decimal
print(st.mean([1, 2, Decimal("3")]))
print(type(st.mean([1, 2, Decimal("3")])))
2
<class 'decimal.Decimal'>
Measured: Decimal mixed with plain int works fine and the result stays a Decimal -- it's specifically float and Fraction that Decimal can't be coerced with inside statistics, matching the fact that plain Python arithmetic rejects Decimal+float too (though, as shown above, Fraction+float arithmetic works directly while Fraction+Decimal arithmetic doesn't -- yet both raise inside statistics.mean() once Decimal is one of the two types). A data pipeline that stores measurements as plain float in one place and switches to Decimal for a "more precise" column elsewhere will crash the first time both land in the same statistics.mean() call, with an error message that doesn't mention "Decimal" or "float" in the way a normal arithmetic error would make obvious at a glance -- read the full message, or convert everything to one exact type (e.g. Decimal via each value's str(), not float()) before calling statistics functions on mixed-provenance data.
Frequently asked questions
Can statistics.mean() mix Decimal and float values in the same list?
No -- we measured statistics.mean([1.5, Decimal('0.5')]) raising TypeError: don't know how to coerce float and Decimal. Convert everything to the same type first, ideally Decimal via str() rather than float() to avoid baking in binary rounding error.
Can statistics.mean() mix Fraction and Decimal values?
No -- we measured statistics.mean([Fraction(1,2), Decimal('0.5')]) raising TypeError: don't know how to coerce Decimal and Fraction, even though both types individually mix fine with float and int respectively in other combinations.
Why does statistics.mean() raise a different TypeError message than normal arithmetic?
statistics has its own internal _coerce() function for combining mixed numeric types across a dataset, separate from Python's normal operator dispatch. It raises "don't know how to coerce <type> and <type>" instead of the standard "unsupported operand type(s) for +" message you'd see from direct arithmetic like 1.5 + Decimal('0.5').
Does statistics.mean() support mixing Fraction and float?
Yes -- we measured statistics.mean([Fraction(1, 2), 0.5]) returning 0.5 as a float with no error, unlike the Decimal combinations which raise TypeError. Only Decimal mixed with float or Fraction specifically fails to coerce.
Written with AI assistance. Measured on 2026-10-04 with Python 3.13.5's built-in statistics module: called mean() on lists mixing int, float, Fraction, and Decimal values in different combinations, and compared the exact error to Python's normal arithmetic TypeError. PlainHeaders is our extension; Python's statistics, fractions, and decimal modules are built in.