Skip to content

Fix inconsistent return type for statistics median_grouped() gh-92531 #92533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 49 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bbd2da9
Merge pull request #1 from python/master
rhettinger Mar 16, 2021
74bdf1b
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
6c53f1a
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
a487c4f
.
rhettinger Mar 24, 2021
eb56423
.
rhettinger Mar 25, 2021
cc7ba06
.
rhettinger Mar 26, 2021
d024dd0
.
rhettinger Apr 22, 2021
b10f912
merge
rhettinger May 5, 2021
fb6744d
merge
rhettinger May 6, 2021
7f21a1c
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 15, 2021
7da42d4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Aug 25, 2021
e31757b
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
f058a6f
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
1fc29bd
Merge branch 'main' of github.com:python/cpython
rhettinger Sep 4, 2021
e5c0184
Merge branch 'main' of github.com:python/cpython
rhettinger Oct 30, 2021
3c86ec1
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
96675e4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Nov 9, 2021
de558c6
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
418a07f
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 14, 2021
ea23a8b
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 21, 2021
ba248b7
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 27, 2021
9bc1df1
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
d4466ba
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
a89f02e
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 8, 2021
aae9a5f
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 10, 2021
7ba634b
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 1, 2022
4910ba3
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 5, 2022
0e8d64a
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 9, 2022
7e49f3e
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 10, 2022
6257706
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 18, 2022
2fb7e2c
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
b345021
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
cbb9ace
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
7642c27
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
2320c28
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 30, 2022
9dbc96c
Merge branch 'main' of github.com:python/cpython
rhettinger Feb 3, 2022
c7c9c0f
Merge branch 'main' of github.com:python/cpython
rhettinger Apr 7, 2022
23ed5e3
Merge branch 'main' of github.com:python/cpython
rhettinger Apr 18, 2022
c4f5cd1
Merge branch 'main' of github.com:python/cpython
rhettinger Apr 20, 2022
6b22356
Merge branch 'main' of github.com:python/cpython
rhettinger Apr 20, 2022
adeb3b6
Merge branch 'main' of github.com:python/cpython
rhettinger May 3, 2022
0c8451a
Merge branch 'main' of github.com:python/cpython
rhettinger May 4, 2022
3e57704
Merge branch 'main' of github.com:python/cpython
rhettinger May 4, 2022
39c3b68
Merge branch 'main' of github.com:python/cpython
rhettinger May 6, 2022
e4beac5
Simplify main code path. Inputs were already going to be converted to…
rhettinger May 9, 2022
2845755
Fix return type bug
rhettinger May 9, 2022
2112502
Tweak the wording a bit
rhettinger May 9, 2022
4e21d53
Another wording tweak
rhettinger May 9, 2022
90bde24
Add blurb
rhettinger May 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def median_high(data):
return data[n // 2]


def median_grouped(data, interval=1):
def median_grouped(data, interval=1.0):
"""Estimates the median for numeric data binned around the midpoints
of consecutive, fixed-width intervals.

Expand Down Expand Up @@ -650,35 +650,34 @@ def median_grouped(data, interval=1):
by exact multiples of *interval*. This is essential for getting a
correct result. The function does not check this precondition.

Inputs may be any numeric type that can be coerced to a float during
the interpolation step.

"""
data = sorted(data)
n = len(data)
if n == 0:
if not n:
raise StatisticsError("no median for empty data")
elif n == 1:
return data[0]

# Find the value at the midpoint. Remember this corresponds to the
# midpoint of the class interval.
x = data[n // 2]

# Generate a clear error message for non-numeric data
for obj in (x, interval):
if isinstance(obj, (str, bytes)):
raise TypeError(f'expected a number but got {obj!r}')

# Using O(log n) bisection, find where all the x values occur in the data.
# All x will lie within data[i:j].
i = bisect_left(data, x)
j = bisect_right(data, x, lo=i)

# Coerce to floats, raising a TypeError if not possible
try:
interval = float(interval)
x = float(x)
except ValueError:
raise TypeError(f'Value cannot be converted to a float')

# Interpolate the median using the formula found at:
# https://www.cuemath.com/data/median-of-grouped-data/
try:
L = x - interval / 2 # The lower limit of the median interval.
except TypeError:
# Coerce mixed types to float.
L = float(x) - float(interval) / 2
L = x - interval / 2.0 # Lower limit of the median interval
cf = i # Cumulative frequency of the preceding interval
f = j - i # Number of elements in the median internal
return L + interval * (n / 2 - cf) / f
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,12 @@ def test_repeated_single_value(self):
data = [x]*count
self.assertEqual(self.func(data), float(x))

def test_single_value(self):
# Override method from AverageMixin.
# Average of a single value is the value as a float.
for x in (23, 42.5, 1.3e15, Fraction(15, 19), Decimal('0.28')):
self.assertEqual(self.func([x]), float(x))

def test_odd_fractions(self):
# Test median_grouped works with an odd number of Fractions.
F = Fraction
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The statistics.median_grouped() function now always return a float.
Formerly, it did not convert the input type when for sequences of length
one.