Hi, I am interested in the use of numpy with native python objects, like so: In [91]: import collections In [92]: testContainer = collections.namedtuple('testContainer', 'att1 att2 att3') In [93]: test1 = testContainer(1, 2, 3) In [94]: test2 = testContainer(4, 5, 6) In [95]: test1 Out[95]: testContainer(att1=1, att2=2, att3=3) In [96]: test2 Out[96]: testContainer(att1=4, att2=5, att3=6) In [97]: x = np.empty((2,2), dtype=object) In [98]: x[0,0] = test1 In [99]: x[1,1] = test2 In [100]: x Out[100]: array([[testContainer(att1=1, att2=2, att3=3), None], [None, testContainer(att1=4, att2=5, att3=6)]], dtype=object) Does anyone know if it possible to form a mask using the attributes of the objects stored in the ndarray? After a few failed attempts I am left wondering if I should use a prepared dtype instead. Cheers, Nathan.
On Mon, Nov 14, 2011 at 9:08 PM, Nathan Faggian
I am interested in the use of numpy with native python objects, like so:
In [91]: import collections In [92]: testContainer = collections.namedtuple('testContainer', 'att1 att2 att3') In [93]: test1 = testContainer(1, 2, 3) In [94]: test2 = testContainer(4, 5, 6) In [95]: test1 Out[95]: testContainer(att1=1, att2=2, att3=3) In [96]: test2 Out[96]: testContainer(att1=4, att2=5, att3=6) In [97]: x = np.empty((2,2), dtype=object) In [98]: x[0,0] = test1 In [99]: x[1,1] = test2 In [100]: x Out[100]: array([[testContainer(att1=1, att2=2, att3=3), None], [None, testContainer(att1=4, att2=5, att3=6)]], dtype=object)
Does anyone know if it possible to form a mask using the attributes of the objects stored in the ndarray?
Maybe something like this: def attr_equal(attribute, value): def _func(x): if x is None: return False else: return getattr(x, attribute) == value return np.vectorize(_func) print attr_equal('att2', 5)(x) print attr_equal('att1', 1)(x) Regards Stéfan
participants (2)
-
Nathan Faggian
-
Stéfan van der Walt