10BC0 Merge pull request #7129 from madphysicist/percentile-midpoint-interp… · numpy/numpy@ea97756 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea97756

Browse files
committed
Merge pull request #7129 from madphysicist/percentile-midpoint-interpolation
BUG: Fixed 'midpoint' interpolation of np.percentile in odd cases.
2 parents e36d7ca + 9ec694b commit ea97756

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

doc/release/1.12.0-notes.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ default order for arrays that are now both.
3232

3333
``MaskedArray`` takes view of data **and** mask when slicing
3434
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35-
3635
XXX
3736

3837

38+
``np.percentile`` 'midpoint' interpolation method fixed for exact indices
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
'midpoint' interpolator now gives the same result as 'lower' and 'higher' when
41+
the two coincide. Previous behavior of 'lower' + 0.5 is fixed.
42+
43+
3944
DeprecationWarning to error
4045
~~~~~~~~~~~~~~~~~~~~~~~~~~~
4146

numpy/lib/function_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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