I’m having trouble getting numpy.ma to work with my data. I’m sure I’ve used it before to mask blank values but can’t figure out how. Here’s a snippet of code that shows the problem I’m having.
import numpy as np
import numpy.ma as ma
x = np.array([[0.0, 1.1, '', 2.2, ''],[3.3,'', 4.4, '', 5.5]])
for index, value in np.ndenumerate(x):
if value == '':
x[index] = None
x = ma.masked_values(x, None)
print x
This prints:
[['0' '1' 'N' '2' 'N']
['3' 'N' '4' 'N' '5']]
What I’m trying to do is get a masked array of floats with any missing values masked out. The final print should produce:
[[0.0 1.0 -- 2.0 --]
[3.0 -- 4.0 -- 5.0]]
If you replaced empty strings with
NaN, the following would work:The easiest place to change the
''tonp.nanis before you place them innp.array.