Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Fixed issue that occurs when trying to take the median of a list of masked arrays. issue #10757 #10909

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions numpy/ma/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,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,
Expand Down
19 changes: 19 additions & 0 deletions numpy/ma/tests/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,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(object):

Expand Down