[SciPy-User] Masking multiple fields in a structured timeseriesobject.

Pierre GM pgmdevlist@gmail....
Tue Jan 12 14:10:48 CST 2010


On Jan 12, 2010, at 1:39 PM, Dharhas Pothina wrote:
> Sorry I'm still having trouble figuring out how to do multiple masking on a limited date range rather than the entire series. For a simpler example, look at the below ts construct:
> 
>>>> ndtype=[('name','|S3'),('v1',float),('v2',float)]
>>>> series=ts.time_series([("ABBC",1.1,10.),("ABD",2.2,20.),("ABBE",3.3,30),("ABBF",4.4,40),("ABG",5.5,50),("ABH",6.6,60)],dtype=ndtype, start_date=ts.now('D'))
>>>> sdate = series.dates[1]
>>>> edate = series.dates[4]
> 
> now I want to mask the v1 value between sdate and edate that contain 'BB' in the name and v1<4 and v2>10. ie the 3rd element ("ABBE",3.3,30) would become ("ABBE",--,30)


Well, if I do your job for you, where's the fun ;) ? Seriously, why don't you build several masks and combine them as you want ?
* Make a mask M1 for the 'BB' in name (use an approach similar to which I posted last time)
* Make a mask M2 that tests the values:
>>>  M2=(_series['v1']<4)&(_series['v2']>10)
* Make a mask M3 that test for the dates:
>>>  M3=(series.dates>=sdate)&(series.dates<edate)
* Combine the masks to make one that satisfies all the conditions:
>>> Mall=np.array(M1&M2&M3, dtype=bool)
(we need to make sure that Mall is a boolean ndarray, and not an array of 0 and 1 else we mess up fancy indexing)
* Mask 'v1' according to the new mask:
>>> series['v1'][Mall]=ma.masked

Notes: 
* you use 3 characters for name, but try to put strings with 4 characters. Expect problems.
* When you build the masks, use series.series as much as you can (that'll save you some time)



More information about the SciPy-User mailing list