diff options
author | Roy Smart <roytsmart@gmail.com> | 2022-12-02 16:09:33 -0700 |
---|---|---|
committer | Roy Smart <roytsmart@gmail.com> | 2022-12-05 15:53:32 -0700 |
commit | 91432a36a3611c2374ea9e2d45592f0ac5e71adb (patch) | |
tree | c8f5686e420e3365bc7af432476c353ef289218c /numpy/ma/tests/test_extras.py | |
parent | f74b3720ca6148574da40ed96d4712e221ee84bd (diff) | |
download | numpy-91432a36a3611c2374ea9e2d45592f0ac5e71adb.tar.gz |
BUG: `keepdims=True` is ignored if `out` is not `None` in `numpy.median()`, `numpy.percentile()`, and `numpy.quantile()`.
Closes #22714, #22544.
Diffstat (limited to 'numpy/ma/tests/test_extras.py')
-rw-r--r-- | numpy/ma/tests/test_extras.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index 3c95e25ea..38603fb84 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -12,6 +12,7 @@ import itertools import pytest import numpy as np +from numpy.core.numeric import normalize_axis_tuple from numpy.testing import ( assert_warns, suppress_warnings ) @@ -989,6 +990,34 @@ class TestMedian: assert_(r is out) assert_(type(r) is MaskedArray) + @pytest.mark.parametrize( + argnames='axis', + argvalues=[ + None, + 1, + (1, ), + (0, 1), + (-3, -1), + ] + ) + def test_keepdims_out(self, axis): + mask = np.zeros((3, 5, 7, 11), dtype=bool) + # Randomly set some elements to True: + w = np.random.random((4, 200)) * np.array(mask.shape)[:, None] + w = w.astype(np.intp) + mask[tuple(w)] = np.nan + d = masked_array(np.ones(mask.shape), mask=mask) + if axis is None: + shape_out = (1,) * d.ndim + else: + axis_norm = normalize_axis_tuple(axis, d.ndim) + shape_out = tuple( + 1 if i in axis_norm else d.shape[i] for i in range(d.ndim)) + out = masked_array(np.empty(shape_out)) + result = median(d, axis=axis, keepdims=True, out=out) + assert result is out + assert_equal(result.shape, shape_out) + def test_single_non_masked_value_on_axis(self): data = [[1., 0.], [0., 3.], |