MCPcopy Create free account
hub / github.com/numpy/numpy / fromrecords

Function fromrecords

numpy/core/records.py:684–765  ·  view source on GitHub ↗

Create a recarray from a list of records in text form. Parameters ---------- recList : sequence data in the same field may be heterogeneous - they will be promoted to the highest data type. dtype : data-type, optional valid dtype for all arrays shape : in

(recList, dtype=None, shape=None, formats=None, names=None,
                titles=None, aligned=False, byteorder=None)

Source from the content-addressed store, hash-verified

682
683@set_module("numpy.rec")
684def fromrecords(recList, dtype=None, shape=None, formats=None, names=None,
685 titles=None, aligned=False, byteorder=None):
686 """Create a recarray from a list of records in text form.
687
688 Parameters
689 ----------
690 recList : sequence
691 data in the same field may be heterogeneous - they will be promoted
692 to the highest data type.
693 dtype : data-type, optional
694 valid dtype for all arrays
695 shape : int or tuple of ints, optional
696 shape of each array.
697 formats, names, titles, aligned, byteorder :
698 If `dtype` is ``None``, these arguments are passed to
699 `numpy.format_parser` to construct a dtype. See that function for
700 detailed documentation.
701
702 If both `formats` and `dtype` are None, then this will auto-detect
703 formats. Use list of tuples rather than list of lists for faster
704 processing.
705
706 Returns
707 -------
708 np.recarray
709 record array consisting of given recList rows.
710
711 Examples
712 --------
713 >>> r=np.core.records.fromrecords([(456,'dbe',1.2),(2,'de',1.3)],
714 ... names='col1,col2,col3')
715 >>> print(r[0])
716 (456, 'dbe', 1.2)
717 >>> r.col1
718 array([456, 2])
719 >>> r.col2
720 array(['dbe', 'de'], dtype='<U3')
721 >>> import pickle
722 >>> pickle.loads(pickle.dumps(r))
723 rec.array([(456, 'dbe', 1.2), ( 2, 'de', 1.3)],
724 dtype=[('col1', '<i8'), ('col2', '<U3'), ('col3', '<f8')])
725 """
726
727 if formats is None and dtype is None: # slower
728 obj = sb.array(recList, dtype=object)
729 arrlist = [sb.array(obj[..., i].tolist()) for i in range(obj.shape[-1])]
730 return fromarrays(arrlist, formats=formats, shape=shape, names=names,
731 titles=titles, aligned=aligned, byteorder=byteorder)
732
733 if dtype is not None:
734 descr = sb.dtype((record, dtype))
735 else:
736 descr = format_parser(formats, names, titles, aligned, byteorder).dtype
737
738 try:
739 retval = sb.array(recList, dtype=descr)
740 except (TypeError, ValueError):
741 # NumPy 1.19.0, 2020-01-01

Callers 1

arrayFunction · 0.70

Calls 8

format_parserClass · 0.85
recarrayClass · 0.85
warnMethod · 0.80
fromarraysFunction · 0.70
tolistMethod · 0.45
dtypeMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected