As shown in the answer to the question Convert python list with None values to numpy array with nan values, it is straightforward to initialize a masked numpy array from a list with None values if we enforce the dtype=float. Those float values get converted to nan and we can simply do:
ma.masked_invalid(np.array(a, dtype=float), copy=False)
This however will not work for int like:
ma.masked_invalid(np.array(a, dtype=int), copy=False)
since the intermediate np.array will not be created with None values (there is no int nan).
What is the most efficient way to initialize a masked array based on Python list of ints that also contains None values in such way that those None values become masked?
The most elegant solution I have found so far (and it is not elegant at all) is to initialize a masked array of type float
and convert it to int
afterwards:
ma.masked_invalid(np.array(a, dtype=float), copy=False).astype(int)
This generates a proper NP array where None
values in the initial array a
are masked. For instance, for:
a = [1, 2, 3, None, 4]
ma.masked_invalid(np.array(a, dtype=float), copy=False).astype(int)
we get:
masked_array(data = [1 2 3 -- 4],
mask = [False False False True False],
fill_value = 999999)
Also, the actual masked int values become min int, i.e.
ma.masked_invalid(np.array(column, dtype=float), copy=False).astype(int).data
gives:
array([ 1, 2, 3,
-9223372036854775808, 4])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With