How does numpy determine the array data type when it contains multiple dtypes?

后端 未结 1 448
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 04:00

I am trying to do hands on the numpy, i cam across following datatype when used inbuilt method dtype.Following the few results i have got. Can you please explain what it me

相关标签:
1条回答
  • 2020-12-07 04:52

    Numpy's array objects that are PyArrayObject types have a NPY_PRIORITY attribute that denotes the priority of the type in which should be considered as the array's dtype in cases that it contains items with heterogeneous data types. You can access to this priority using PyArray_GetPriority API which Returns the __array_priority__ attribute (converted to a double) of obj or def if no attribute of that name exists. In this case Unicode has a more priority than integer type and that's why a1.dtype returns U11.

    Now, regarding the U11 or in general U#, it consists of two parts. The U which denotes a Unicode dtype and the # denotes the number of elements it can hold. This may be different in different platforms though.

    In [45]: a1.dtype
    Out[45]: dtype('<U21')  # 64bit Linux
    
    In [46]: a1.dtype.type  # The type object used to instantiate a scalar of this data-type. 
    Out[46]: numpy.str_
    
    In [49]: a1.dtype.itemsize
    Out[49]: 84 # 21 * 4
    

    Read more info in greater details about string types and other datatype objects in documentation https://docs.scipy.org/doc/numpy-1.14.0/reference/arrays.dtypes.html#data-type-objects-dtype.

    0 讨论(0)
提交回复
热议问题