Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.dtype

classnumpy.dtype[source]

Create a data type object.

A numpy array is homogeneous, and contains elements described by adtype object. A dtype object can be constructed from differentcombinations of fundamental numeric types.

Parameters:

obj

Object to be converted to a data type object.

align : bool, optional

Add padding to the fields to match what a C compiler would outputfor a similar C-struct. Can beTrue only ifobj is a dictionaryor a comma-separated string. If a struct dtype is being created,this also sets a sticky alignment flagisalignedstruct.

copy : bool, optional

Make a new copy of the data-type object. IfFalse, the resultmay just be a reference to a built-in data-type object.

See also

result_type

Examples

Using array-scalar type:

>>>np.dtype(np.int16)dtype('int16')

Structured type, one field name ‘f1’, containing int16:

>>>np.dtype([('f1',np.int16)])dtype([('f1', '<i2')])

Structured type, one field named ‘f1’, in itself containing a structuredtype with one field:

>>>np.dtype([('f1',[('f1',np.int16)])])dtype([('f1', [('f1', '<i2')])])

Structured type, two fields: the first field contains an unsigned int, thesecond an int32:

>>>np.dtype([('f1',np.uint),('f2',np.int32)])dtype([('f1', '<u4'), ('f2', '<i4')])

Using array-protocol type strings:

>>>np.dtype([('a','f8'),('b','S10')])dtype([('a', '<f8'), ('b', '|S10')])

Using comma-separated field formats. The shape is (2,3):

>>>np.dtype("i4, (2,3)f8")dtype([('f0', '<i4'), ('f1', '<f8', (2, 3))])

Using tuples.int is a fixed type, 3 the field’s shape.voidis a flexible type, here of size 10:

>>>np.dtype([('hello',(np.int,3)),('world',np.void,10)])dtype([('hello', '<i4', 3), ('world', '|V10')])

Subdivideint16 into 2int8‘s, called x and y. 0 and 1 arethe offsets in bytes:

>>>np.dtype((np.int16,{'x':(np.int8,0),'y':(np.int8,1)}))dtype(('<i2', [('x', '|i1'), ('y', '|i1')]))

Using dictionaries. Two fields named ‘gender’ and ‘age’:

>>>np.dtype({'names':['gender','age'],'formats':['S1',np.uint8]})dtype([('gender', '|S1'), ('age', '|u1')])

Offsets in bytes, here 0 and 25:

>>>np.dtype({'surname':('S25',0),'age':(np.uint8,25)})dtype([('surname', '|S25'), ('age', '|u1')])

Attributes

base
descrPEP3118 interface description of the data-type.
fieldsDictionary of named fields defined for this data type, orNone.
hasobjectBoolean indicating whether this dtype contains any reference-counted objects in any fields or sub-dtypes.
isalignedstructBoolean indicating whether the dtype is a struct which maintains field alignment.
isbuiltinInteger indicating how this dtype relates to the built-in dtypes.
isnativeBoolean indicating whether the byte order of this dtype is native to the platform.
metadata
nameA bit-width name for this data-type.
namesOrdered list of field names, orNone if there are no fields.
ndimNumber of dimensions of the sub-array if this data type describes a sub-array, and0 otherwise.
shapeShape tuple of the sub-array if this data type describes a sub-array, and() otherwise.
strThe array-protocol typestring of this data-type object.
subdtypeTuple(item_dtype,shape) if thisdtype describes a sub-array, and None otherwise.

Methods

newbyteorder([new_order])Return a new dtype with a different byte order.

Previous topic

Data type objects (dtype)

Next topic

numpy.dtype.base

  • © Copyright 2008-2009, The Scipy community.
  • Last updated on Jun 10, 2017.
  • Created usingSphinx 1.5.3.

[8]ページ先頭

©2009-2025 Movatter.jp