python - Filling NumPy array with object instances? -


i trying add object instances of astropy angles numpy array , , getting error:

valueerror: setting array element sequence.

the angle objects this:

<angle 1.2557346257567 deg>

if put them in normal python list, get:

s = [<angle 1.2562500714928306 deg>,      <angle 1.2562500714928306 deg>,      <angle 1.2562500714928306 deg>] len(s) >>> 3 len(s[0]) >>> typeerror: 'angle' object scalar value has no len() 

so, first question is, in way python object sequence? and, since numpy arrays need initialized specific dimensions, how find "length" of object can load them numpy array?

i don't have astrop package , don't know details of angle object. can make python , numpy observations.

<angle 1.2557346257567 deg> string representation of object, produced __repr__ method.

the typeerror len(s[0]) means object not __len__ method. not subclass of list, nor specialized numpy array. check docs. may have way of yielding numeric value or values.

you don't how trying 'add' angle array, or kind of array. if array numeric, e.g. dtype=float, doing

a[0]= <angle...>  

is not going work, because angle not number, nor produce 1 - @ least not without method. need tell target array supposed contain. numbers, angle objects?

you can build array contains objects. np.array(s) might work. when list contains dictionary objects.

in [67]: ll out[67]: [{1: 2}, {}, {'a': 1, 'b': 2}]  in [68]: np.array(ll) out[68]: array([{1: 2}, {}, {'b': 2, 'a': 1}], dtype=object) 

but np.array([...]) can tricky use, since it's designed produce multidimensional array of numbers - if possible.

or might have make a = np.zeros((3,),dtype=object) array, , assign values individually, a[0]=s[0]. such object array variant on list. it's not 2d array of numbers.

from previous astropy question:

how covert np.ndarray astropy.coordinates.angle class?

angle(angles_quantity).wrap_at('360d').value # returns simple ndarray again. 

=================

digging docs , astropy github code, see angle subclass of quantity subclass of ndarray. tries, in effect array (or scalar) appropriate unit definition.

the .value method returns self.view(np.ndarray), unless it's shape 0d, in case returns self.item(), scalar value.

so should able define angle multiple values, e.g.

angle([1.23, 1.24, 1.25]) 

i don't know if can join multiple angles one

angle(s) 

where s list of angle objects, might work. don't see angle versions of concatenate or stack.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -