python - How may numpy return a length one object array? -


in numpy 1.8.2, when index numpy array of fixed length strings , ask 1 value, numpy array of length 1, supporting numpy operations. so:

import numpy np strs = np.array(('aa', 'bbb', 'c'), dtype=np.dtype('|s4')) print type(strs[(0,)]) 

i get

<type 'numpy.string_'> 

if same array of objects:

strs = np.array(('aa', 'bbb', 'c'), dtype=np.dtype('object')) print type(strs[(0,)]) 

i get

<type 'str'> 

and numpy specific property/method (e.g. .shape) returns exception

how may ensure numpy returns me length 1 array of objects slicing?

from data type objects documentation:

"an item extracted array, e.g., indexing, python object type scalar type associated data type of array.

note scalar types not dtype objects, though can used in place of 1 whenever data type specification needed in numpy."

by using dtype=np.dtype('object'), therefore creating numpy array filled scalar types. numpy array defined as: "homogeneous, , contains elements described dtype object. dtype object can constructed different combinations of fundamental numeric types."

numpy array docs


Comments