[SciPy-user] Masked array question
Robert Ferrell
ferrell at diablotech.com
Mon Jun 8 12:59:41 EDT 2009
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))
but that doesn't work. It returns
/Library/Frameworks/Python.framework/Versions/2.5.2001/lib/python2.5/
site-packages/numpy-1.3.0.dev5934-py2.5-macosx-10.3-i386.egg/numpy/ma/
core.py in __new__(cls, data, mask, dtype, copy, subok, ndmin,
fill_value, keep_mask, hard_mask, flag, shrink, **options)
1257 shrink = flag
1258 # Process data............
-> 1259 _data = np.array(data, dtype=dtype, copy=copy,
subok=True, ndmin=ndmin)
1260 _baseclass = getattr(data, '_baseclass', type(_data))
1261 # Check that we'ew not erasing the mask..........
<type 'exceptions.ValueError'>: setting an array element with a
sequence.
Is there a way to do this (easily)? What I can do is first convert
the strings to floats, and set the empty strings to some fill value,
and then pass that to masked_array. Is that the best solution? Or is
there some method that I've missed?
thanks,
-robert
More information about the SciPy-User
mailing list