title: creating a masked array from text fields
body: <p>The <a href="http://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html" rel="nofollow noreferrer">numpy documentation</a> shows an example of masking existing values with <code>ma.masked</code> a posteriori (after array creation), or creating a masked array from an list of what seem to be valid data types (integer if <code>dtype=int</code>). I am trying to read in data from a file (and requires some text manipulation) but at some point I will have a list of lists (or tuples) containing strings from which I want to make a numeric (float) array. </p>
  
  <p>An example of the data might be <code>textdata='1\t2\t3\n4\t\t6'</code> (typical flat text format after cleaning).</p>
  
  <p>One problem I have is that missing values may be encoded as '', which when trying to convert to float using the dtype argument, will tell me </p>
  
  <pre><code>ValueError: setting an array element with a sequence. 
  </code></pre>
  
  <p>So I've created this function</p>
  
  <pre><code>def makemaskedarray(X,missing='',fillvalue='-999.',dtype=float):
      arr = lambda x: x==missing and fillvalue or x    
      mask = lambda x: x==missing and 1 or 0
      triple = dict(zip(('data','mask','dtype'),
                        zip(*[(map(arr,x),map(mask,x)) for x in X])+
                        [dtype]))
      return ma.array(**triple)
  </code></pre>
  
  <p>which seems to do the trick:</p>
  
  <pre><code>&gt;&gt;&gt; makemaskedarray([('1','2','3'),('4','','6')])
  masked_array(data =
   [[1.0 2.0 3.0]
   [4.0 -- 6.0]],
               mask =
   [[False False False]
   [False  True False]],
         fill_value = 1e+20)
  </code></pre>
  
  <p>Is this the way to do it? Or there is a built-in function?</p>