[Numpy-discussion] more recfunctions, structured array help

Pierre GM pgmdevlist@gmail....
Tue Dec 8 16:50:26 CST 2009


On Dec 8, 2009, at 5:32 PM, John [H2O] wrote:
> Pierre GM-2 wrote:
>> 
>> 
>> 
>> masked_where is a function that requires 2 arguments.
>> If you try to mask a whole record, you can try something like
>>>>> x = ma.array([('a',1),('b',2)],dtype=[('','|S1'),('',float)])
>>>>> x[x['f0']=='a'] = ma.masked
>> For an individual field, try something like
>>>>> x['f1'][x['f1']=='b'] = ma.masked
>> 
>> 
> 
> 
> Just some more detail, here's what I'm working on:

Did you check scikits.timeseries ? Might be a solution if you have data indexed in time

> 
> def mk_COarray(rD,datetimevec):
>    """ rD is a previous record array, but I add the datetime vector """
>    codata =
> np.column_stack((np.array(datetime),rD.lon,rD.lat,rD.elv,rD.co))
>    codata = np.ma.array(codata)
>    codata_masked = np.ma.masked_where(codata==-9999.,codata)
>    codata =
> np.rec.fromrecords(codata_masked,names='datetime,lon,lat,elv,co')
>    return codata, codata_masked

OK, I gonna have to guess again:
codata is a regular ndarray, not structured ? Then you don't have to transform it into a masked array
codata=...
codata_masked = np.ma.masked_values(codata,-9999.)

Then you transform codata into a np.recarray... But why not transforming codata_masked ?

It is hard to help you, because I don't know the actual structure you use. Once again, please give a self contained example. The first two entries of codata would be enough.


> Plotting the arrays out of this:
> In [128]: cd,cdm = mk_COarray(rD,datetimevec)
> In [129]: plt.plot(cd.datetime,cd.co,label='raw');
> plt.plot(cdm[:,0],cdm[:,4],label='masked')
> 
> I get the following image, where you can see that the codata which is
> created from the codata_masked seems to not be masked????


Er... You can check whether codata_masked is masked by checking if some entries of its mask are True (codata_masked.mask.any()). 
Given your graph, I'd say yes, codata_masked is actually masked: see how you have a gap in your green curve when the blue one plummets into negative? That's likely where your elevation was -9999., I'd say.  


More information about the NumPy-Discussion mailing list