diff options
| -rw-r--r-- | doc/release/1.12.0-notes.rst | 7 | ||||
| -rw-r--r-- | numpy/lib/function_base.py | 2 | ||||
| -rw-r--r-- | numpy/lib/tests/test_function_base.py | 6 |
3 files changed, 12 insertions, 3 deletions
diff --git a/doc/release/1.12.0-notes.rst b/doc/release/1.12.0-notes.rst index ee4e2d24a..d6089c311 100644 --- a/doc/release/1.12.0-notes.rst +++ b/doc/release/1.12.0-notes.rst @@ -32,10 +32,15 @@ default order for arrays that are now both. ``MaskedArray`` takes view of data **and** mask when slicing ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - XXX +``np.percentile`` 'midpoint' interpolation method fixed for exact indices +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +'midpoint' interpolator now gives the same result as 'lower' and 'higher' when +the two coincide. Previous behavior of 'lower' + 0.5 is fixed. + + DeprecationWarning to error ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 844c069c0..fbe41442b 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3541,7 +3541,7 @@ def _percentile(a, q, axis=None, out=None, elif interpolation == 'higher': indices = ceil(indices).astype(intp) elif interpolation == 'midpoint': - indices = floor(indices) + 0.5 + indices = 0.5 * (floor(indices) + ceil(indices)) elif interpolation == 'nearest': indices = around(indices).astype(intp) elif interpolation == 'linear': diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index d6a838f3a..56466c1ea 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2055,7 +2055,7 @@ def compare_results(res, desired): assert_array_equal(res[i], desired[i]) -class TestScoreatpercentile(TestCase): +class TestPercentile(TestCase): def test_basic(self): x = np.arange(8) * 0.5 @@ -2104,6 +2104,10 @@ class TestScoreatpercentile(TestCase): def test_midpoint(self): assert_equal(np.percentile(range(10), 51, interpolation='midpoint'), 4.5) + assert_equal(np.percentile(range(11), 51, + interpolation='midpoint'), 5.5) + assert_equal(np.percentile(range(11), 50, + interpolation='midpoint'), 5) def test_nearest(self): assert_equal(np.percentile(range(10), 51, |
