python - Returned dtype of numpy vectorized function -


i have issue regarding dtype of returned numpy array of vectorized function. function returns number, fraction. strangely position of fraction seems influence returned dtype. want type object if function returns fraction.

import numpy np fractions import fraction  foo = lambda x: fraction(1, 3) if x < 0.5 else 1 foo_vectorized = np.vectorize(foo)  foo_vectorized([1, 0.3]) # returns array([1, 0]) foo_vectorized([0.3, 1]) # returns array([fraction(1, 3), 1], dtype=object) 

is bug or expected work this? use numpy 1.9.2 on enthought canopy python 2.7.6.

thanks explanation!

that documentation states:

"the output type determined evaluating first element of input, unless specified."

you can specify desired output type via otypes arg, e.g.:

 np.vectorize(foo, otypes=[np.object]) 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -