8000 BUG: Fix masked median multiple masked arrays (#21999) · mattip/numpy@6b8d55e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b8d55e

Browse files
bsipoczjsclose
andauthored
BUG: Fix masked median multiple masked arrays (numpy#21999)
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 numpy#10757 for more information. Co-authored-by: jsclose <jsclose@umich.edu>
1 parent 7a93aa6 commit 6b8d55e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

numpy/ma/extras.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,10 @@ def median(a, axis=None, out=None, overwrite_input=False, keepdims=False):
723723
fill_value=1e+20)
724724
725725
"""
726-
if not hasattr(a, 'mask'):
726+
727+
a = np.ma.asarray(a)
728+
729+
if a.mask is np.ma.nomask:
727730
m = np.median(getdata(a, subok=True), axis=axis,
728731
out=out, overwrite_input=overwrite_input,
729732
keepdims=keepdims)

numpy/ma/tests/test_extras.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,25 @@ def test_object(self):
11601160
o[2] = np.nan
11611161
assert_(type(np.ma.median(o.astype(object))), float)
11621162

1163+
def test_list_of_masked_array(self):
1164+
data1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
1165+
masked1 = np.ma.masked_where(data1 == 4, data1)
1166+
data2 = np.array([[8, 7, 6, 5], [4, 3, 2, 1]])
1167+
masked2 = np.ma.masked_where(data2 == 4, data2)
1168+
list = [masked1, masked2]
1169+
median_masked_list = np.ma.median(list, axis=0).data
1170+
assert_equal(median_masked_list,
1171+
np.array([[4.5, 4.5, 4.5, 5], [5, 4.5, 4.5, 4.5]]))
1172+
1173+
def test_list_of_masked_array_no_axis(self):
1174+
data1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
1175+
masked1 = np.ma.masked_where(data1 == 2, data1)
1176+
data2 = np.array([[8, 7, 6, 5], [4, 3, 2, 1]])
1177+
masked2 = np.ma.masked_where(data2 == 5, data2)
1178+
list = [masked1, masked2]
1179+
median_masked_list = np.ma.median(list)
1180+
assert_equal(median_masked_list, 4.5)
1181+
11631182

11641183
class TestCov:
11651184

0 commit comments

Comments
 (0)
0