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

Function structured_to_unstructured

numpy/lib/recfunctions.py:938–1064  ·  view source on GitHub ↗

Converts an n-D structured array into an (n+1)-D unstructured array. The new array will have a new last dimension equal in size to the number of field-elements of the input array. If not supplied, the output datatype is determined from the numpy type promotion rules applied to all

(arr, dtype=None, copy=False, casting='unsafe')

Source from the content-addressed store, hash-verified

936
937@array_function_dispatch(_structured_to_unstructured_dispatcher)
938def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'):
939 """
940 Converts an n-D structured array into an (n+1)-D unstructured array.
941
942 The new array will have a new last dimension equal in size to the
943 number of field-elements of the input array. If not supplied, the output
944 datatype is determined from the numpy type promotion rules applied to all
945 the field datatypes.
946
947 Nested fields, as well as each element of any subarray fields, all count
948 as a single field-elements.
949
950 Parameters
951 ----------
952 arr : ndarray
953 Structured array or dtype to convert. Cannot contain object datatype.
954 dtype : dtype, optional
955 The dtype of the output unstructured array.
956 copy : bool, optional
957 If true, always return a copy. If false, a view is returned if
958 possible, such as when the `dtype` and strides of the fields are
959 suitable and the array subtype is one of `np.ndarray`, `np.recarray`
960 or `np.memmap`.
961
962 .. versionchanged:: 1.25.0
963 A view can now be returned if the fields are separated by a
964 uniform stride.
965
966 casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
967 See casting argument of `numpy.ndarray.astype`. Controls what kind of
968 data casting may occur.
969
970 Returns
971 -------
972 unstructured : ndarray
973 Unstructured array with one more dimension.
974
975 Examples
976 --------
977
978 >>> from numpy.lib import recfunctions as rfn
979 >>> a = np.zeros(4, dtype=[('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)])
980 >>> a
981 array([(0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.]),
982 (0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.])],
983 dtype=[('a', '<i4'), ('b', [('f0', '<f4'), ('f1', '<u2')]), ('c', '<f4', (2,))])
984 >>> rfn.structured_to_unstructured(a)
985 array([[0., 0., 0., 0., 0.],
986 [0., 0., 0., 0., 0.],
987 [0., 0., 0., 0., 0.],
988 [0., 0., 0., 0., 0.]])
989
990 >>> b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)],
991 ... dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')])
992 >>> np.mean(rfn.structured_to_unstructured(b[['x', 'z']]), axis=-1)
993 array([ 3. , 5.5, 9. , 11. ])
994
995 """

Callers 3

inspectMethod · 0.90
apply_along_fieldsFunction · 0.85

Calls 10

_get_fields_and_offsetsFunction · 0.85
_common_strideFunction · 0.85
absFunction · 0.85
wrapFunction · 0.85
astypeMethod · 0.80
allFunction · 0.50
sumFunction · 0.50
minFunction · 0.50
dtypeMethod · 0.45
viewMethod · 0.45

Tested by 2

inspectMethod · 0.72