Calling masked_array (the class constructor) and the masked_where function both seem to do exactly the same thing, in terms of being able to construct a numpy masked array given the data and mask values. When would you use one or the other?
>>> import numpy as np
>>> import numpy.ma as MA
>>> vals = np.array([0,1,2,3,4,5])
>>> cond = vals > 3
>>> vals
array([0, 1, 2, 3, 4, 5])
>>> cond
array([False, False, False, False, True, True], dtype=bool)
>>> MA.masked_array(data=vals, mask=cond)
masked_array(data = [0 1 2 3 -- --],
mask = [False False False False True True],
fill_value = 999999)
>>> MA.masked_where(cond, vals)
masked_array(data = [0 1 2 3 -- --],
mask = [False False False False True True],
fill_value = 999999)
The optional argument copy to masked_where (its only documented optional argument) is also supported by masked_array, so I don't see any options that are unique to masked_where. Although the converse is not true (e.g. masked_where doesn't support dtype), I don't understand the purpose of masked_where as a separate function.
You comment:
I don't think we can help you without more details on what's different.
For example if I try the obvious inconsistency, that of length, I get different error messages:
The test for the
wheremessage is obvious from the code that Corralien shows.The
Masked_Arrayclass definition has this test:I'd expect the same message only if the shapes made it past the
wheretest, but were caught by the Class's test. If so that should be obvious in the full error traceback.Here's an example that fails on the
where, but passes the base.The base class can handle cases where the
conddiffers inshape, but matches insize(total number of elements). It tries to reshape it. A scalarcondpasses both though the exact test differs.Based on my reading of the code, I can't conceive of a difference that passes the
where, but not the base.All the Masked Array code is python readable (see the link the other answer). While there is one base class definition, there are a number of constructor or helper functions, as the
wheredocs makes clear. I won't worry too much about which function(s) to use, especially if you aren't trying to push the boundaries of what's logical.Masked arrays, while a part of
numpyfor a long time, does not get a whole lot of use, at least judging by relative lack of SO questions. I suspectpandashas largely replaced it when dealing with data that can have missing values (e.g. time series).