diff options
| author | Charles Harris <charlesr.harris@gmail.com> | 2016-01-31 19:59:59 -0700 |
|---|---|---|
| committer | Charles Harris <charlesr.harris@gmail.com> | 2016-01-31 19:59:59 -0700 |
| commit | ea9775606c8303a3fd65fdf21a4a02846cef971e (patch) | |
| tree | 46aa84bc84684e2e7c373952b3d967a6cad34dd2 | |
| parent | e36d7cab82570b82df3ff0dbc1fc066ae1632768 (diff) | |
| parent | 9ec694b69a231a8de43032711c657d253edbed9d (diff) | |
| download | numpy-ea9775606c8303a3fd65fdf21a4a02846cef971e.tar.gz | |
Merge pull request #7129 from madphysicist/percentile-midpoint-interpolation
BUG: Fixed 'midpoint' interpolation of np.percentile in odd cases.
| -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 1d9030ec6..a49d02a1a 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3543,7 +3543,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 878d00bdf..c3483b032 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2066,7 +2066,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 @@ -2115,6 +2115,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, |
