import numpy as np
import numpy.ma as ma
"""This operates as expected with one value masked"""
a = [0., 1., 1.e20, 9.]
error_value = 1.e20
b = ma.masked_values(a, error_value)
print b
"""This does not, all values are masked """
d = [0., 1., 'NA', 9.]
error_value = 'NA'
e = ma.masked_values(d, error_value)
print e
How can I use ‘nan’, ‘NA’, ‘None’, or some similar value to indicate missing data?
Are you getting your data from a text file or similar? If so, I’d suggest using the
genfromtxtfunction directly to specify your masked value:I think the problem in your example is that the python list you’re using to initialize the numpy array has heterogeneous types (floats and a string). The values are coerced to a strings in a numpy array, but the
masked_valuesfunction uses floating point equality yielding the strange results.Here’s one way to overcome this by creating an array with object dtype:
You may prefer the first solution since the result has a float dtype.