[Numpy-discussion] question/request with Numeric compress and putmask
Travis E. Oliphant
oliphant at ee.byu.edu
Mon Feb 2 08:33:01 CST 2004
Vineet Jain wrote:
> I'm using the Numeric arrays for financial data elements. I'm interfacing
> with an external c library which does not support invalid elements. To get
> around this I maintain a separate mask array in my python class which
> denotes which elements are valid. I then use the compress function with the
> mask array to get an array with valid elements which I pass to the c
> What I would like to do is:
> putmask(full_return_value, my_mask, return_value)
> where return_value is treated like a list so that every 1 that is found in
> my_mask the next element in return_value is used. Is their anything that
> matches this?
There are functions in SciPy to handle exactly this situation.
>>> from scipy import *
insert(arr, mask, vals)
Similar to putmask arr[mask] = vals but 1d array vals has the
same number of elements as the non-zero values of mask. Inverse of extract.
Elements of ravel(condition) where ravel(condition) is true (1-d)
Equivalent of compress(ravel(condition), ravel(arr))
Thus, for your problem I would do:
financial_data = [10, 11, 22, 33, INVALID, INVALID, 44, 55]
my_mask = [1, 1, 1, 1, 0, 0, 1, 1]
compressed_data = extract(my_mask, financial_data)
return_value = some_c_function(compressed_data)
insert(financial_data, my_mask, return_value)
These functions are in scipy_base and so you only need to install
scipy_base to get them.
More information about the Numpy-discussion