Superposes arrays fields by fields Parameters ---------- arrays : array or sequence Sequence of input arrays. defaults : dictionary, optional Dictionary mapping field names to the corresponding default values. usemask : {True, False}, optional Whethe
(arrays, defaults=None, usemask=True, asrecarray=False,
autoconvert=False)
| 1316 | |
| 1317 | @array_function_dispatch(_stack_arrays_dispatcher) |
| 1318 | def stack_arrays(arrays, defaults=None, usemask=True, asrecarray=False, |
| 1319 | autoconvert=False): |
| 1320 | """ |
| 1321 | Superposes arrays fields by fields |
| 1322 | |
| 1323 | Parameters |
| 1324 | ---------- |
| 1325 | arrays : array or sequence |
| 1326 | Sequence of input arrays. |
| 1327 | defaults : dictionary, optional |
| 1328 | Dictionary mapping field names to the corresponding default values. |
| 1329 | usemask : {True, False}, optional |
| 1330 | Whether to return a MaskedArray (or MaskedRecords is |
| 1331 | `asrecarray==True`) or a ndarray. |
| 1332 | asrecarray : {False, True}, optional |
| 1333 | Whether to return a recarray (or MaskedRecords if `usemask==True`) |
| 1334 | or just a flexible-type ndarray. |
| 1335 | autoconvert : {False, True}, optional |
| 1336 | Whether automatically cast the type of the field to the maximum. |
| 1337 | |
| 1338 | Examples |
| 1339 | -------- |
| 1340 | >>> from numpy.lib import recfunctions as rfn |
| 1341 | >>> x = np.array([1, 2,]) |
| 1342 | >>> rfn.stack_arrays(x) is x |
| 1343 | True |
| 1344 | >>> z = np.array([('A', 1), ('B', 2)], dtype=[('A', '|S3'), ('B', float)]) |
| 1345 | >>> zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], |
| 1346 | ... dtype=[('A', '|S3'), ('B', np.double), ('C', np.double)]) |
| 1347 | >>> test = rfn.stack_arrays((z,zz)) |
| 1348 | >>> test |
| 1349 | masked_array(data=[(b'A', 1.0, --), (b'B', 2.0, --), (b'a', 10.0, 100.0), |
| 1350 | (b'b', 20.0, 200.0), (b'c', 30.0, 300.0)], |
| 1351 | mask=[(False, False, True), (False, False, True), |
| 1352 | (False, False, False), (False, False, False), |
| 1353 | (False, False, False)], |
| 1354 | fill_value=(b'N/A', 1.e+20, 1.e+20), |
| 1355 | dtype=[('A', 'S3'), ('B', '<f8'), ('C', '<f8')]) |
| 1356 | |
| 1357 | """ |
| 1358 | if isinstance(arrays, ndarray): |
| 1359 | return arrays |
| 1360 | elif len(arrays) == 1: |
| 1361 | return arrays[0] |
| 1362 | seqarrays = [np.asanyarray(a).ravel() for a in arrays] |
| 1363 | nrecords = [len(a) for a in seqarrays] |
| 1364 | ndtype = [a.dtype for a in seqarrays] |
| 1365 | fldnames = [d.names for d in ndtype] |
| 1366 | # |
| 1367 | dtype_l = ndtype[0] |
| 1368 | newdescr = _get_fieldspec(dtype_l) |
| 1369 | names = [n for n, d in newdescr] |
| 1370 | for dtype_n in ndtype[1:]: |
| 1371 | for fname, fdtype in _get_fieldspec(dtype_n): |
| 1372 | if fname not in names: |
| 1373 | newdescr.append((fname, fdtype)) |
| 1374 | names.append(fname) |
| 1375 | else: |