[Numpy-discussion] more recfunctions, structured array help
Pierre GM
pgmdevlist at gmail.com
Tue Dec 8 21:19:59 EST 2009
On Dec 8, 2009, at 7:11 PM, John [H2O] wrote:
> My apologies for adding confusing. In answer to your first question. Yes at
> one point I tried playing with scikits.timeseries... there were some issues
> at the time that prevented me from working with it, maybe I should revisit.
What kind of issues ?
>
> As best I can I'll provide a full example.
Except that you forgot to give a sample of the input arrays ;)
>
> Here's what I have:
>
> def mk_COarray(rD,dtvector,mask=None):
> codata =
> np.column_stack((np.array(dtvector),rD.lon,rD.lat,rD.elv,rD.co))
> print type(codata)
>
> if mask:
> codata_masked = np.ma.masked_where(codata==mask,codata,copy=False)
> # Create record array from codata_masked
> else:
> codata_masked = codata
> codata =
> np.rec.fromrecords(codata_masked,names='datetime,lon,lat,elv,co')
> #Note the above is just for debugging, and below I return masked and
> unmasked arrays
> return codata, codata_masked
>
>
> In [162]: codata,codata_masked =mk_COarray(rD,dtvec,mask=-9999.)
> In [163]: type(codata); type(codata_masked)
> Out[163]: <class 'numpy.core.records.recarray'>
> Out[163]: <class 'numpy.ma.core.MaskedArray'>
> In [164]: codata[0]
> Out[164]: (datetime.datetime(2008, 4, 6, 11, 38, 37, 760000),
> 20.327100000000002, 67.8215, 442.60000000000002, -9999.0)
>
> In [165]: codata_masked[0]
> Out[165]:
> masked_array(data = [2008-04-06 11:38:37.760000 20.3271 67.8215 442.6 --],
> mask = [False False False False True],
> fill_value = ?)
>
>
> So then, the plot above will be the same. codata is the blue line
> (codata_masked converted into a rec array), whereas for debugging, I also
> return codata_masked (and it is plotted green).
>
> In my prior post I used the variables cd and cdm which refer to codata and
> codata_masked.
>
> I know this isn't terribly clear, but hopefully enough so to let me know how
> to create a masked record array ;)
>
Your structured ndarray:
>>> x=np.array([('aaa',1,2,30),('bbb',2,4,40)], dtype=[('f0',np.object),('f1',int),('f2',int),('f3',float)])
Make a MaskedRecords:
>>> x = x.view(np.ma.mrecords.mrecarray)
Mask the whole records where field 'f3' > 30
>>> x[x['f3'] > 30] = ma.masked
Mask the 'f1' field where it is equal to 1
>>> x['f1'][x['f1']==1]=ma.masked
More information about the NumPy-Discussion
mailing list