summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/function_base.py12
-rw-r--r--numpy/lib/tests/test_function_base.py21
2 files changed, 29 insertions, 4 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 7d825136c..5504a0f99 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -2964,9 +2964,15 @@ def percentile(a, q, axis=None, out=None, overwrite_input=False):
# handle sequence of q's without calling sort multiple times
def _compute_qth_percentile(sorted, q, axis, out):
if not isscalar(q):
- return [_compute_qth_percentile(sorted, qi, axis, out)
- for qi in q]
- q = q / 100.0
+ p = [_compute_qth_percentile(sorted, qi, axis, None)
+ for qi in q]
+
+ if out is not None:
+ out.flat = p
+
+ return p
+
+ q = q / 100.0
if (q < 0) or (q > 1):
raise ValueError, "percentile must be either in the range [0,100]"
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index 4949eba5d..73fcc5621 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -968,7 +968,26 @@ def compare_results(res, desired):
def test_percentile_list():
- assert_equal(np.percentile([1,2,3], 0), 1)
+ assert_equal(np.percentile([1, 2, 3], 0), 1)
+
+def test_percentile_out():
+ x = np.array([1, 2, 3])
+ y = np.zeros((3,))
+ p = (1, 2, 3)
+ np.percentile(x, p, out=y)
+ assert_equal(y, np.percentile(x, p))
+
+ x = np.array([[1, 2, 3],
+ [4, 5, 6]])
+
+ y = np.zeros((3, 3))
+ np.percentile(x, p, axis=0, out=y)
+ assert_equal(y, np.percentile(x, p, axis=0))
+
+ y = np.zeros((3, 2))
+ np.percentile(x, p, axis=1, out=y)
+ assert_equal(y, np.percentile(x, p, axis=1))
+
if __name__ == "__main__":
run_module_suite()