statistics: correlation() Crashes on Pure Decimal Input (tested)
statistics.mean() and variance() both work with Decimal data, which makes it reasonable to expect the newer correlation(), covariance(), and linear_regression() functions (added in Python 3.10) to work too. We measured all three on pure Decimal input, and none of them run at all.
1. mean() and variance() handle Decimal input fine
from decimal import Decimal
import statistics as st
x = [Decimal("1"), Decimal("2"), Decimal("3")]
print(st.mean(x))
print(st.variance(x))
2
1
Measured: both functions run cleanly on a list of Decimal values and return correct results, giving no reason to expect a problem elsewhere in the same module.
2. correlation(), covariance(), and linear_regression() all raise TypeError on the exact same data
from decimal import Decimal
import statistics as st
x = [Decimal("1"), Decimal("2"), Decimal("3")]
y = [Decimal("2"), Decimal("4"), Decimal("6")]
try:
st.correlation(x, y)
except TypeError as e:
print("correlation:", e)
try:
st.covariance(x, y)
except TypeError as e:
print("covariance:", e)
try:
st.linear_regression(x, y)
except TypeError as e:
print("linear_regression:", e)
correlation: unsupported operand type(s) for -: 'decimal.Decimal' and 'float'
covariance: unsupported operand type(s) for -: 'decimal.Decimal' and 'float'
linear_regression: unsupported operand type(s) for -: 'decimal.Decimal' and 'float'
Measured: all three raise the identical TypeError, not StatisticsError -- so code that wraps these calls in except StatisticsError to catch bad-input cases (modeled on the module's own documented exception type) won't catch this at all. Decimal values flat-out can't be mixed with float in arithmetic, and that's exactly what breaks here.
3. The cause is internal: these functions compute their mean via the float-coercing path
from decimal import Decimal
import statistics as st
x = [Decimal("1"), Decimal("2"), Decimal("3")]
print(st.fmean(x)) # float, by design (see the fmean guide)
print(type(st.fmean(x)))
2.0
Measured: correlation(), covariance(), and linear_regression() subtract each value from the data's mean internally -- and that mean comes out as a plain float (the same float-coercion fmean() always applies). Subtracting a float from a Decimal is where Python's arithmetic draws the line, so the crash traces back to the same internal design choice covered in the fmean()/Decimal guide, just surfacing as an outright crash here instead of silent precision loss. Converting the data to float or Fraction first avoids the crash -- Fraction at least runs (see the separate covariance()-always-returns-float guide), though it still won't preserve exactness.
Frequently asked questions
Does statistics.correlation() work with Decimal input?
No -- we measured correlation() raising TypeError: unsupported operand type(s) for -: 'decimal.Decimal' and 'float' on pure Decimal data, even though mean() and variance() work fine on the same data.
Is the error from statistics.correlation() on Decimal input a StatisticsError?
No -- we measured it as a plain TypeError, not the module's own StatisticsError, so an except StatisticsError clause around the call won't catch it.
Why does correlation() crash on Decimal but mean() doesn't?
correlation(), covariance(), and linear_regression() all compute an internal mean using the same float-coercing logic as fmean(), then subtract that float mean from each Decimal value -- and Python's Decimal type doesn't support arithmetic with float at all, which raises the TypeError.
How do I compute correlation on Decimal data without it crashing?
We measured converting the data to float or Fraction first as working around the crash -- Fraction input runs without error, though it still returns a plain float result rather than an exact one (a separate, documented quirk of these three functions).
Written with AI assistance. Measured on 2026-10-04 with Python 3.13.5's built-in statistics module: called mean(), variance(), correlation(), covariance(), and linear_regression() on the same pure-Decimal data. PlainHeaders is our extension; Python's statistics module is built in.