ó 2ÄÈ[c @`s!dZddlmZmZmZddlZddlZddlZddlm Z ddlm Z ddl mZmZmZmZddlmZd d d gZe jZid d 6dd6dd6d d6dd6dd6dd6dd6d d 6dd6dd6dd6dd6dd6Ze jZd„Zd efd„ƒYZd e jfd„ƒYZd efd„ƒYZeeeeee ed„Z!eeeeee ed„Z"eedeeee ed „Z#d!„Z$eedeeee ed"„Z%eedeeeee ee&d#„ Z'dS($s† Record Arrays ============= Record arrays expose the fields of structured arrays as properties. Most commonly, ndarrays contain elements of a single type, e.g. floats, integers, bools etc. However, it is possible for elements to be combinations of these using structured types, such as:: >>> a = np.array([(1, 2.0), (1, 2.0)], dtype=[('x', int), ('y', float)]) >>> a array([(1, 2.0), (1, 2.0)], dtype=[('x', '>> a['x'] array([1, 1]) >>> a['y'] array([ 2., 2.]) Record arrays allow us to access fields as properties:: >>> ar = np.rec.array(a) >>> ar.x array([1, 1]) >>> ar.y array([ 2., 2.]) i(tdivisiontabsolute_importtprint_functionNi(tnumeric(t numerictypes(t isfileobjtbytestlongtunicode(tget_printoptionstrecordtrecarrayt format_parsert>tbt>> np.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'], ... ['T1', 'T2', 'T3']).dtype dtype([(('T1', 'col1'), '>> np.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'], ... []).dtype dtype([('col1', '>> np.format_parser(['f8', 'i4', 'a5'], [], []).dtype dtype([('f0', '!ss %% %ds: %%ss (R%R(tmaxRRaR3(R&R(tmaxlentrowstfmtRg((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pytpprints   $( RIRJRKRQRORWRbRdRl(((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pyR Ùs     c B`szeZdZdZdZd d dd d d d d edd„ Zd„Zd„Zd„Zd „Z d „Z d d „Z RS( s2Construct an ndarray that allows field access using attributes. Arrays may have a data-types containing fields, analogous to columns in a spread sheet. An example is ``[(x, int), (y, float)]``, where each entry in the array is a pair of ``(int, float)``. Normally, these attributes are accessed using dictionary lookups such as ``arr['x']`` and ``arr['y']``. Record arrays allow the fields to be accessed as members of the array, using ``arr.x`` and ``arr.y``. Parameters ---------- shape : tuple Shape of output array. dtype : data-type, optional The desired data-type. By default, the data-type is determined from `formats`, `names`, `titles`, `aligned` and `byteorder`. formats : list of data-types, optional A list containing the data-types for the different columns, e.g. ``['i4', 'f8', 'i4']``. `formats` does *not* support the new convention of using types directly, i.e. ``(int, float, int)``. Note that `formats` must be a list, not a tuple. Given that `formats` is somewhat limited, we recommend specifying `dtype` instead. names : tuple of str, optional The name of each column, e.g. ``('x', 'y', 'z')``. buf : buffer, optional By default, a new array is created of the given shape and data-type. If `buf` is specified and is an object exposing the buffer interface, the array will use the memory from the existing buffer. In this case, the `offset` and `strides` keywords are available. Other Parameters ---------------- titles : tuple of str, optional Aliases for column names. For example, if `names` were ``('x', 'y', 'z')`` and `titles` is ``('x_coordinate', 'y_coordinate', 'z_coordinate')``, then ``arr['x']`` is equivalent to both ``arr.x`` and ``arr.x_coordinate``. byteorder : {'<', '>', '='}, optional Byte-order for all fields. aligned : bool, optional Align the fields in memory as the C-compiler would. strides : tuple of ints, optional Buffer (`buf`) is interpreted according to these strides (strides define how many bytes each array element, row, column, etc. occupy in memory). offset : int, optional Start reading buffer (`buf`) from this offset onwards. order : {'C', 'F'}, optional Row-major (C-style) or column-major (Fortran-style) order. Returns ------- rec : recarray Empty array of the given shape and type. See Also -------- rec.fromrecords : Construct a record array from data. record : fundamental data-type for `recarray`. format_parser : determine a data-type from formats, names, titles. Notes ----- This constructor can be compared to ``empty``: it creates a new record array but does not fill it with data. To create a record array from data, use one of the following methods: 1. Create a standard ndarray and convert it to a record array, using ``arr.view(np.recarray)`` 2. Use the `buf` keyword. 3. Use `np.rec.fromrecords`. Examples -------- Create an array with two fields, ``x`` and ``y``: >>> x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) >>> x array([(1.0, 2), (3.0, 4)], dtype=[('x', '>> x['x'] array([ 1., 3.]) View the array as a record array: >>> x = x.view(np.recarray) >>> x.x array([ 1., 3.]) >>> x.y array([2, 4]) Create a new, empty record array: >>> np.recarray((2,), ... dtype=[('x', int), ('y', float), ('z', int)]) #doctest: +SKIP rec.array([(-1073741821, 1.2249118382103472e-301, 24547520), (3471280, 1.2134086255804012e-316, 0)], dtype=[('x', '>> x1=np.array([1,2,3,4]) >>> x2=np.array(['a','dd','xyz','12']) >>> x3=np.array([1.1,2,3,4]) >>> r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c') >>> print(r[1]) (2, 'dd', 2.0) >>> x1[1]=34 >>> r.a array([1, 2, 3, 4]) is*item in the array list must be an ndarray.R.s>mismatch between the number of fields and the number of arrayss array-shape mismatch in array %dN(R4tasarrayR0RuR2RŽRrR1RR%R=R3R(R RBR$Rt enumeratetndimR R(t arrayListR%RuR'R(R)R*R+txR_RHRBtparsedtd0tnntkt testshapet_arrayR((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pyt fromarrays=sB"           cC`s|dkrš|dkrštj|dtƒ}gt|jdƒD]%} tj|d| fjƒƒ^qA} t| d|d|d|d|d|d |ƒS|dk r¾tjt |fƒ} nt |||||ƒj } ytj|d| ƒ} WnÔt t fk rÈ|dks |d kr/t|ƒ}nt|ttfƒrP|f}nt|ƒd krqt d ƒ‚nt|| ƒ} x+t| jƒD]}t||ƒ| |>> r=np.core.records.fromrecords([(456,'dbe',1.2),(2,'de',1.3)], ... names='col1,col2,col3') >>> print(r[0]) (456, 'dbe', 1.2) >>> r.col1 array([456, 2]) >>> r.col2 array(['dbe', 'de'], dtype='|S3') >>> import pickle >>> print(pickle.loads(pickle.dumps(r))) [(456, 'dbe', 1.2) (2, 'de', 1.3)] R%iÿÿÿÿ.R'RuR(R)R*R+iisCan only deal with 1-d array.sxfromrecords expected a list of tuples, may have received a list of lists instead. In the future that will raise an errort stackleveliN(R0R4tarrayRxRRuttolistR›R%R R R$RyR1RR2RŽRR R‰R<twarningstwarnt FutureWarningRZ(trecListR%RuR'R(R)R*R+R_RtarrlistRHtretvalRšR˜R^((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pyt fromrecords~s8?    c C`sÃ|dkr'|dkr'tdƒ‚n|dk rEtj|ƒ} nt|||||ƒj} | j} |dks|dks|dkr¤t|ƒ|| }nt|| d|d|ƒ} | S(sM create a (read-only) record array from binary data contained in a stringsMust have dtype= or formats=iiÿÿÿÿRvRpN( R0R1R4R%R R$titemsizeRR ( t datastringR%RuRpR'R(R)R*R+RHR¦Rš((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pyt fromstringÃs  $cC`sdy|jƒ}Wn+tk r=tjj|jƒ|jƒSXtj|ƒ}|j|jƒ}|S(N( tfilenoRXtostpathtgetsizeRgttelltfstattst_size(tfdtfntstR‰((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pytget_remaining_size×s c C`sÇ|dks|dkr!d}n!t|ttfƒrB|f}nd} t|tƒrod} t|dƒ}n|dkrŽ|j|dƒnt|ƒ} |dk r¸tj |ƒ} nt |||||ƒj } | j } tj |ƒjƒ} | | }|dkrOt|ƒ}| | ||jdƒ>> from tempfile import TemporaryFile >>> a = np.empty(10,dtype='f8,i4,a5') >>> a[5] = (0.5,10,'abcde') >>> >>> fd=TemporaryFile() >>> a = a.newbyteorder('<') >>> a.tofile(fd) >>> >>> fd.seek(0) >>> r=np.core.records.fromfile(fd, formats='f8,i4,a5', shape=10, ... byteorder='<') >>> print(r[5]) (0.5, 10, 'abcde') >>> r.shape (10,) iiÿÿÿÿitrbs:Not enough bytes left in file for specified shape and types%Didn't read as many bytes as expectedN(iÿÿÿÿ(R0R2RŽRR=topentseekR³R4R%R R$R¦RtprodRtindexR<R1R treadintotdatatIOErrortclose(R°R%RuRpR'R(R)R*R+RgR‰RHR¦t shapeprodt shapesizetnbytesRšt nbytesread((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pytfromfileàsB               c  C`s t|tdƒtfƒs't|ƒrN|dkrN|dkrNtdƒ‚ni} |dk rrtj|ƒ}nS|dk rœt||||| ƒj }n)i|d6|d6|d6|d6| d6} |dkr |dkrìtdƒ‚nt ||d|d |d |ƒSt|t ƒr6t ||d |d || St|t tfƒr™t|d tt fƒr}t|d |d || St|d |d || Snmt|t ƒrô|dk rÕ|j|krÕ|j|ƒ} n|} | rð| jƒ} n| St|ƒrt|d |d |d |ƒSt|tƒr€|dk rX|j|krX|j|ƒ} n|} | rs| jƒ} n| jt ƒSt|ddƒ} | dks®t| tƒ r½tdƒ‚ntj|ƒ}|dk rù|j|krù|j|ƒ}n|jt ƒSdS(s=Construct a record array from a wide-variety of objects. sIMust define formats (or dtype) if object is None, string, or an open fileR'R(R)R*R+s"Must define a shape if obj is NoneRvRpRqRuiR%t__array_interface__sUnknown input typeN(R2R;R0R=RR1R4R%R R$R RR¨RR<R¥R›RZtcopyRÁRrRatdictR(R_R%RuRpRqR'R(R)R*R+RÃtkwdstnewt interface((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pyR%s^'         ((RKt __future__RRRR~RªRŸR-RR4RRUt numpy.compatRRRRt arrayprintR t__all__RrRFttypeDicttnumfmtR RxR RVR R R0RLR›R¥R¨R³RÁtTrueR(((s1/tmp/pip-build-fiC0ax/numpy/numpy/core/records.pyt$sR   "     „Wÿ @ D  D