Skip to content

Commit d5b7bba

Browse files
authored
Statistics internals: Make fewer calls to _coerce() when data types are mixed (GH-31619)
1 parent 7496f95 commit d5b7bba

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Lib/statistics.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
from itertools import groupby, repeat
138138
from bisect import bisect_left, bisect_right
139139
from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum
140+
from functools import reduce
140141
from operator import mul
141142
from collections import Counter, namedtuple, defaultdict
142143

@@ -183,11 +184,12 @@ def _sum(data):
183184
allowed.
184185
"""
185186
count = 0
187+
types = set()
188+
types_add = types.add
186189
partials = {}
187190
partials_get = partials.get
188-
T = int
189191
for typ, values in groupby(data, type):
190-
T = _coerce(T, typ) # or raise TypeError
192+
types_add(typ)
191193
for n, d in map(_exact_ratio, values):
192194
count += 1
193195
partials[d] = partials_get(d, 0) + n
@@ -199,6 +201,7 @@ def _sum(data):
199201
else:
200202
# Sum all the partial sums using builtin sum.
201203
total = sum(Fraction(n, d) for d, n in partials.items())
204+
T = reduce(_coerce, types, int) # or raise TypeError
202205
return (T, total, count)
203206

204207

@@ -214,11 +217,12 @@ def _ss(data, c=None):
214217
T, total, count = _sum((d := x - c) * d for x in data)
215218
return (T, total, count)
216219
count = 0
220+
types = set()
221+
types_add = types.add
217222
sx_partials = defaultdict(int)
218223
sxx_partials = defaultdict(int)
219-
T = int
220224
for typ, values in groupby(data, type):
221-
T = _coerce(T, typ) # or raise TypeError
225+
types_add(typ)
222226
for n, d in map(_exact_ratio, values):
223227
count += 1
224228
sx_partials[d] += n
@@ -236,6 +240,7 @@ def _ss(data, c=None):
236240
# This formula has poor numeric properties for floats,
237241
# but with fractions it is exact.
238242
total = (count * sxx - sx * sx) / count
243+
T = reduce(_coerce, types, int) # or raise TypeError
239244
return (T, total, count)
240245

241246

0 commit comments

Comments
 (0)