8000 BUG: Fixed 'midpoint' interpolation of np.percentile in odd cases. · charris/numpy@d195078 · GitHub
[go: up one dir, main page]

Skip to content

Commit d195078

Browse files
madphysicistcharris
authored andcommitted
BUG: Fixed 'midpoint' interpolation of np.percentile in odd cases.
Backport of numpy#7129
1 parent 84e3b8b commit d195078

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

numpy/lib/function_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ def select(condlist, choicelist, default=0):
10271027
dtype = np.result_type(*choicelist)
10281028

10291029
# Convert conditions to arrays and broadcast conditions and choices
1030-
# as the shape is needed for the result. Doing it seperatly optimizes
1030+
# as the shape is needed for the result. Doing it separately optimizes
10311031
# for example when all choices are scalars.
10321032
condlist = np.broadcast_arrays(*condlist)
10331033
choicelist = np.broadcast_arrays(*choicelist)
@@ -1249,7 +1249,7 @@ def gradient(f, *varargs, **kwargs):
12491249

12501250
# Convert datetime64 data into ints. Make dummy variable `y`
12511251
# that is a view of ints if the data is datetime64, otherwise
1252-
# just set y equal to the the array `f`.
1252+
# just set y equal to the array `f`.
12531253
if f.dtype.char in ["M", "m"]:
12541254
y = f.view('int64')
12551255
else:
@@ -3543,7 +3543,7 @@ def _percentile(a, q, axis=None, out=None,
35433543
elif interpolation == 'higher':
35443544
indices = ceil(indices).astype(intp)
35453545
elif interpolation == 'midpoint':
3546-
indices = floor(indices) + 0.5
3546+
indices = 0.5 * (floor(indices) + ceil(indices))
35473547
elif interpolation == 'nearest':
35483548
indices = around(indices).astype(intp)
35493549
elif interpolation == 'linear':

numpy/lib/tests/test_function_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,7 @@ def compare_results(res, desired):
20662066
assert_array_equal(res[i], desired[i])
20672067

20682068

2069-
class TestScoreatpercentile(TestCase):
2069+
class TestPercentile(TestCase):
20702070

20712071
def test_basic(self):
20722072
x = np.arange(8) * 0.5
@@ -2115,6 +2115,10 @@ def test_lower_higher(self):
21152115
def test_midpoint(self):
21162116
assert_equal(np.percentile(range(10), 51,
21172117
interpolation='midpoint'), 4.5)
2118+
assert_equal(np.percentile(range(11), 51,
2119+
interpolation='midpoint'), 5.5)
2120+
assert_equal(np.percentile(range(11), 50,
2121+
interpolation='midpoint'), 5)
21182122

21192123
def test_nearest(self):
21202124
assert_equal(np.percentile(range(10), 51,

0 commit comments

Comments
 (0)
0