Nik
Nik

Reputation: 315

Numpy masked array argmax not returning 'masked' on fully masked array?

I have a masked array in numpy.ma, for which all values are masked:

import numpy.ma as ma
arr = ma.array([3,4,10], mask=[True, True, True])

I expect that operations on this array, such as ma.sum should return masked:

>>> ma.sum(arr) is ma.masked
>>> True

And this is indeed True.

But when I use ma.argmax() on the same array, the result is not ma.masked but rather 0

>>> ma.argmax(arr) is ma.masked
>>> False
>>> ma.argmax(arr)
>>> 0

Any ideas? is this a bug, or expected behavior? Ideally, this would return masked. Can anyone think of a good workaround, or am I being silly... Thanks!

Upvotes: 3

Views: 1159

Answers (2)

Janne Karila
Janne Karila

Reputation: 25207

>>> arr[ma.argmax(arr)]
masked

argmax returns the index of the maximum value. You can use the index to get the value. The value is masked.

Because all the values are masked, they are considered equal (to the fill_value), and argmax returns the first index as documented (in the docs of numpy.argmax).

Upvotes: 3

user545424
user545424

Reputation: 16189

np.argmax returns a scalar so it doesn't make sense for it to return a masked array.

From the documentation (emphasis mine):

Returns array of indices of the maximum values along the given axis. Masked values are treated as if they had the value fill_value.

Upvotes: 2

Related Questions