jackson
jackson

Reputation: 486

Numpy: Stacking masked Arrays and calculating min/max

I'm working with masked arrays and I want to calculate the max of different arrays/columns. I have problems, if the whole array is masked.

Example:

import numpy as np

x = np.ma.array(np.array([1,2,3,4,100]),mask=[True,True,True, True, True])
y = 5

print(np.max(np.hstack((x, y))))
print np.max((np.max(y), np.max(x)))
print(np.max((np.hstack((np.max(x), 5)))))

Results:

100
nan
--

I find the result odd, because the result should be 5. Why is hstack() ignoring the mask of the masked array?

Upvotes: 2

Views: 1204

Answers (1)

behzad.nouri
behzad.nouri

Reputation: 78011

With masked arrays, you need to use masked routines, that is numpy.ma. should precede method name:

>>> np.ma.hstack((x, y))
masked_array(data = [-- -- -- -- -- 5],
             mask = [ True  True  True  True  True False],
       fill_value = 999999)

>>> np.ma.max(np.ma.hstack((x, y)))
5

Upvotes: 3

Related Questions