numpy.rec.array#
- rec.array(obj,dtype=None,shape=None,offset=0,strides=None,formats=None,names=None,titles=None,aligned=False,byteorder=None,copy=True)[source]#
Construct a record array from a wide-variety of objects.
A general-purpose record array constructor that dispatches to theappropriate
recarray
creation function based on the inputs (see Notes).- Parameters:
- objany
Input object. See Notes for details on how various input types aretreated.
- dtypedata-type, optional
Valid dtype for array.
- shapeint or tuple of ints, optional
Shape of each array.
- offsetint, optional
Position in the file or buffer to start reading from.
- stridestuple of ints, optional
Buffer (buf) is interpreted according to these strides (stridesdefine how many bytes each array element, row, column, etc.occupy in memory).
- formats, names, titles, aligned, byteorder
If
dtype
isNone
, these arguments are passed tonumpy.format_parser to construct a dtype. See that function fordetailed documentation.- copybool, optional
Whether to copy the input object (True), or to use a reference instead.This option only applies when the input is an ndarray or recarray.Defaults to True.
- Returns:
- np.recarray
Record array created from the specified object.
Notes
Ifobj is
None
, then call therecarray
constructor. Ifobj is a string, then call thefromstring
constructor. Ifobj is alist or a tuple, then if the first object is anndarray
, callfromarrays
, otherwise callfromrecords
. Ifobj is arecarray
, then make a copy of the data in the recarray(ifcopy=True
) and use the new formats, names, and titles. Ifobjis a file, then callfromfile
. Finally, if obj is anndarray
, thenreturnobj.view(recarray)
, making a copy of the data ifcopy=True
.Examples
>>>a=np.array([[1,2,3],[4,5,6],[7,8,9]])>>>aarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>>np.rec.array(a)rec.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=int64)
>>>b=[(1,1),(2,4),(3,9)]>>>c=np.rec.array(b,formats=['i2','f2'],names=('x','y'))>>>crec.array([(1, 1.), (2, 4.), (3, 9.)], dtype=[('x', '<i2'), ('y', '<f2')])
>>>c.xarray([1, 2, 3], dtype=int16)
>>>c.yarray([1., 4., 9.], dtype=float16)
>>>r=np.rec.array(['abc','def'],names=['col1','col2'])>>>print(r.col1)abc
>>>r.col1array('abc', dtype='<U3')
>>>r.col2array('def', dtype='<U3')