From a3d4ed192f27b76cf343b330a4d9ced81784cd12 Mon Sep 17 00:00:00 2001 From: jsclose Date: Sun, 15 Apr 2018 14:22:24 -0400 Subject: [PATCH 1/4] ENH: Fixed issue that occurs when trying to take the median of a list of masked arrays. Added a check to see if the input is a list then converts to a masked array. See issue #10757 for more information. --- numpy/ma/extras.py | 4 ++++ numpy/ma/tests/test_extras.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index d2986012b7ac..bc96ac3820cb 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -723,6 +723,10 @@ def median(a, axis=None, out=None, overwrite_input=False, keepdims=False): fill_value=1e+20) """ + + if isinstance(a, list): + a = np.ma.asarray(a) + if not hasattr(a, 'mask'): m = np.median(getdata(a, subok=True), axis=axis, out=out, overwrite_input=overwrite_input, diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index 04bf8cfc2284..4de2fcee1170 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -1160,6 +1160,25 @@ def test_object(self): o[2] = np.nan assert_(type(np.ma.median(o.astype(object))), float) + def test_list_of_masked_array(self): + data1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) + masked1 = np.ma.masked_where(data1 == 4, data1) + data2 = np.array([[8, 7, 6, 5], [4, 3, 2, 1]]) + masked2 = np.ma.masked_where(data2 == 4, data2) + list = [masked1, masked2] + median_masked_list = np.ma.median(list, axis=0).data + assert_equal( median_masked_list , np.array([[4.5, 4.5, 4.5, 5], [5, 4.5, 4.5, 4.5]])) + + + def test_list_of_masked_array_no_axis(self): + data1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) + masked1 = np.ma.masked_where(data1 == 2, data1) + data2 = np.array([[8, 7, 6, 5], [4, 3, 2, 1]]) + masked2 = np.ma.masked_where(data2 == 5, data2) + list = [masked1, masked2] + median_masked_list = np.ma.median(list) + assert_equal(median_masked_list, 4.5) + class TestCov: From 58adc9972262e17c1aaee06938d4bc6c34cd742e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brigitta=20Sip=C5=91cz?= Date: Sat, 16 Jul 2022 15:45:29 -0500 Subject: [PATCH 2/4] Addressing review for PR10909 --- numpy/ma/extras.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index bc96ac3820cb..42ba55171e5b 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -724,10 +724,9 @@ def median(a, axis=None, out=None, overwrite_input=False, keepdims=False): """ - if isinstance(a, list): - a = np.ma.asarray(a) + a = np.ma.asarray(a) - if not hasattr(a, 'mask'): + if a.mask is np.nomask: m = np.median(getdata(a, subok=True), axis=axis, out=out, overwrite_input=overwrite_input, keepdims=keepdims) From 47f6bdfb0e4ba40c6c14e8e359d4bbfe53235bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brigitta=20Sip=C5=91cz?= Date: Sat, 16 Jul 2022 15:46:57 -0500 Subject: [PATCH 3/4] Fix linting --- numpy/ma/tests/test_extras.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index 4de2fcee1170..3637accc39f8 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -1167,8 +1167,8 @@ def test_list_of_masked_array(self): masked2 = np.ma.masked_where(data2 == 4, data2) list = [masked1, masked2] median_masked_list = np.ma.median(list, axis=0).data - assert_equal( median_masked_list , np.array([[4.5, 4.5, 4.5, 5], [5, 4.5, 4.5, 4.5]])) - + assert_equal(median_masked_list, + np.array([[4.5, 4.5, 4.5, 5], [5, 4.5, 4.5, 4.5]])) def test_list_of_masked_array_no_axis(self): data1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) From b24e489ab2923408cc2e2d2ac8788cf266ec34f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brigitta=20Sip=C5=91cz?= Date: Sat, 16 Jul 2022 15:50:34 -0500 Subject: [PATCH 4/4] Fix namespace issue --- numpy/ma/extras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 42ba55171e5b..b7ff1adbc2da 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -726,7 +726,7 @@ def median(a, axis=None, out=None, overwrite_input=False, keepdims=False): a = np.ma.asarray(a) - if a.mask is np.nomask: + if a.mask is np.ma.nomask: m = np.median(getdata(a, subok=True), axis=axis, out=out, overwrite_input=overwrite_input, keepdims=keepdims)