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

Function unstructured_to_structured

numpy/lib/recfunctions.py:1072–1176  ·  view source on GitHub ↗

Converts an n-D unstructured array into an (n-1)-D structured array. The last dimension of the input array is converted into a structure, with number of field-elements equal to the size of the last dimension of the input array. By default all output fields have the input array's dt

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

Source from the content-addressed store, hash-verified

1070
1071@array_function_dispatch(_unstructured_to_structured_dispatcher)
1072def unstructured_to_structured(arr, dtype=None, names=None, align=False,
1073 copy=False, casting='unsafe'):
1074 """
1075 Converts an n-D unstructured array into an (n-1)-D structured array.
1076
1077 The last dimension of the input array is converted into a structure, with
1078 number of field-elements equal to the size of the last dimension of the
1079 input array. By default all output fields have the input array's dtype, but
1080 an output structured dtype with an equal number of fields-elements can be
1081 supplied instead.
1082
1083 Nested fields, as well as each element of any subarray fields, all count
1084 towards the number of field-elements.
1085
1086 Parameters
1087 ----------
1088 arr : ndarray
1089 Unstructured array or dtype to convert.
1090 dtype : dtype, optional
1091 The structured dtype of the output array
1092 names : list of strings, optional
1093 If dtype is not supplied, this specifies the field names for the output
1094 dtype, in order. The field dtypes will be the same as the input array.
1095 align : boolean, optional
1096 Whether to create an aligned memory layout.
1097 copy : bool, optional
1098 See copy argument to `numpy.ndarray.astype`. If true, always return a
1099 copy. If false, and `dtype` requirements are satisfied, a view is
1100 returned.
1101 casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
1102 See casting argument of `numpy.ndarray.astype`. Controls what kind of
1103 data casting may occur.
1104
1105 Returns
1106 -------
1107 structured : ndarray
1108 Structured array with fewer dimensions.
1109
1110 Examples
1111 --------
1112
1113 >>> from numpy.lib import recfunctions as rfn
1114 >>> dt = np.dtype([('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)])
1115 >>> a = np.arange(20).reshape((4,5))
1116 >>> a
1117 array([[ 0, 1, 2, 3, 4],
1118 [ 5, 6, 7, 8, 9],
1119 [10, 11, 12, 13, 14],
1120 [15, 16, 17, 18, 19]])
1121 >>> rfn.unstructured_to_structured(a, dt)
1122 array([( 0, ( 1., 2), [ 3., 4.]), ( 5, ( 6., 7), [ 8., 9.]),
1123 (10, (11., 12), [13., 14.]), (15, (16., 17), [18., 19.])],
1124 dtype=[('a', '<i4'), ('b', [('f0', '<f4'), ('f1', '<u2')]), ('c', '<f4', (2,))])
1125
1126 """
1127 if arr.shape == ():
1128 raise ValueError('arr must have at least one dimension')
1129 n_elem = arr.shape[-1]

Callers 3

inspectMethod · 0.90

Calls 5

_get_fields_and_offsetsFunction · 0.85
astypeMethod · 0.80
sumFunction · 0.50
dtypeMethod · 0.45
viewMethod · 0.45

Tested by 3

inspectMethod · 0.72