Creates a MaskedRecords from a list of records. Parameters ---------- reclist : sequence A list of records. Each element of the sequence is first converted to a masked array if needed. If a 2D array is passed as argument, it is processed line by line dty
(reclist, dtype=None, shape=None, formats=None, names=None,
titles=None, aligned=False, byteorder=None,
fill_value=None, mask=nomask)
| 545 | |
| 546 | |
| 547 | def fromrecords(reclist, dtype=None, shape=None, formats=None, names=None, |
| 548 | titles=None, aligned=False, byteorder=None, |
| 549 | fill_value=None, mask=nomask): |
| 550 | """ |
| 551 | Creates a MaskedRecords from a list of records. |
| 552 | |
| 553 | Parameters |
| 554 | ---------- |
| 555 | reclist : sequence |
| 556 | A list of records. Each element of the sequence is first converted |
| 557 | to a masked array if needed. If a 2D array is passed as argument, it is |
| 558 | processed line by line |
| 559 | dtype : {None, dtype}, optional |
| 560 | Data type descriptor. |
| 561 | shape : {None,int}, optional |
| 562 | Number of records. If None, ``shape`` is defined from the shape of the |
| 563 | first array in the list. |
| 564 | formats : {None, sequence}, optional |
| 565 | Sequence of formats for each individual field. If None, the formats will |
| 566 | be autodetected by inspecting the fields and selecting the highest dtype |
| 567 | possible. |
| 568 | names : {None, sequence}, optional |
| 569 | Sequence of the names of each field. |
| 570 | fill_value : {None, sequence}, optional |
| 571 | Sequence of data to be used as filling values. |
| 572 | mask : {nomask, sequence}, optional. |
| 573 | External mask to apply on the data. |
| 574 | |
| 575 | Notes |
| 576 | ----- |
| 577 | Lists of tuples should be preferred over lists of lists for faster processing. |
| 578 | |
| 579 | """ |
| 580 | # Grab the initial _fieldmask, if needed: |
| 581 | _mask = getattr(reclist, '_mask', None) |
| 582 | # Get the list of records. |
| 583 | if isinstance(reclist, ndarray): |
| 584 | # Make sure we don't have some hidden mask |
| 585 | if isinstance(reclist, MaskedArray): |
| 586 | reclist = reclist.filled().view(ndarray) |
| 587 | # Grab the initial dtype, just in case |
| 588 | if dtype is None: |
| 589 | dtype = reclist.dtype |
| 590 | reclist = reclist.tolist() |
| 591 | mrec = recfromrecords(reclist, dtype=dtype, shape=shape, formats=formats, |
| 592 | names=names, titles=titles, |
| 593 | aligned=aligned, byteorder=byteorder).view(mrecarray) |
| 594 | # Set the fill_value if needed |
| 595 | if fill_value is not None: |
| 596 | mrec.fill_value = fill_value |
| 597 | # Now, let's deal w/ the mask |
| 598 | if mask is not nomask: |
| 599 | mask = np.array(mask, copy=False) |
| 600 | maskrecordlength = len(mask.dtype) |
| 601 | if maskrecordlength: |
| 602 | mrec._mask.flat = mask |
| 603 | elif mask.ndim == 2: |
| 604 | mrec._mask.flat = [tuple(m) for m in mask] |