[SciPy-user] Masked array question

josef.pktd@gmai... josef.pktd@gmai...
Mon Jun 8 12:48:56 CDT 2009


On Mon, Jun 8, 2009 at 1:32 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
>
> On Jun 8, 2009, at 12:59 PM, Robert Ferrell wrote:
>
>> I have a tuple of strings that I want to convert to an array of
>> floats.  Some of the strings are empty, so I thought I could use a
>> masked array to mask out the empty strings.  (In my application, empty
>> string means no data, so ignore.)
>>
>> I tried:
>>
>> np.ma.masked_array(('1.', ' '), mask=(False, True), dtype=(np.float32,
>> np.float32))
>
> As indicated by the error message, it's a pb w/ numpy: it doesn't know
> how to process the '' for float. That you're using masked arrays
> unfortunately doesn't change anything to that
> An idea is then to convert your '' to 'NaN' beforehand and use
> np.ma.fix_invalid on the results, or to define a mask as you wanted.
>

A very indirect way, that doesn't look worth the trouble in this case would be

>>> np.mafromtxt(StringIO(','.join(['1','','2'])), delimiter=',')
masked_array(data = [1.0 -- 2.0],
             mask = [False  True False],
       fill_value = 1e+020)

>>> np.genfromtxt(StringIO(','.join(['1','','2'])), delimiter=',')
array([  1.,  NaN,   2.])

if I understand the new functions correctly.

Josef


More information about the SciPy-user mailing list