Merge arrays field by field. Parameters ---------- seqarrays : sequence of ndarrays Sequence of arrays fill_value : {float}, optional Filling value used to pad missing data on the shorter arrays. flatten : {False, True}, optional Whether to collapse
(seqarrays, fill_value=-1, flatten=False,
usemask=False, asrecarray=False)
| 362 | |
| 363 | @array_function_dispatch(_merge_arrays_dispatcher) |
| 364 | def merge_arrays(seqarrays, fill_value=-1, flatten=False, |
| 365 | usemask=False, asrecarray=False): |
| 366 | """ |
| 367 | Merge arrays field by field. |
| 368 | |
| 369 | Parameters |
| 370 | ---------- |
| 371 | seqarrays : sequence of ndarrays |
| 372 | Sequence of arrays |
| 373 | fill_value : {float}, optional |
| 374 | Filling value used to pad missing data on the shorter arrays. |
| 375 | flatten : {False, True}, optional |
| 376 | Whether to collapse nested fields. |
| 377 | usemask : {False, True}, optional |
| 378 | Whether to return a masked array or not. |
| 379 | asrecarray : {False, True}, optional |
| 380 | Whether to return a recarray (MaskedRecords) or not. |
| 381 | |
| 382 | Examples |
| 383 | -------- |
| 384 | >>> import numpy as np |
| 385 | >>> from numpy.lib import recfunctions as rfn |
| 386 | >>> rfn.merge_arrays((np.array([1, 2]), np.array([10., 20., 30.]))) |
| 387 | array([( 1, 10.), ( 2, 20.), (-1, 30.)], |
| 388 | dtype=[('f0', '<i8'), ('f1', '<f8')]) |
| 389 | |
| 390 | >>> rfn.merge_arrays((np.array([1, 2], dtype=np.int64), |
| 391 | ... np.array([10., 20., 30.])), usemask=False) |
| 392 | array([(1, 10.0), (2, 20.0), (-1, 30.0)], |
| 393 | dtype=[('f0', '<i8'), ('f1', '<f8')]) |
| 394 | >>> rfn.merge_arrays((np.array([1, 2]).view([('a', np.int64)]), |
| 395 | ... np.array([10., 20., 30.])), |
| 396 | ... usemask=False, asrecarray=True) |
| 397 | rec.array([( 1, 10.), ( 2, 20.), (-1, 30.)], |
| 398 | dtype=[('a', '<i8'), ('f1', '<f8')]) |
| 399 | |
| 400 | Notes |
| 401 | ----- |
| 402 | * Without a mask, the missing value will be filled with something, |
| 403 | depending on what its corresponding type: |
| 404 | |
| 405 | * ``-1`` for integers |
| 406 | * ``-1.0`` for floating point numbers |
| 407 | * ``'-'`` for characters |
| 408 | * ``'-1'`` for strings |
| 409 | * ``True`` for boolean values |
| 410 | * XXX: I just obtained these values empirically |
| 411 | """ |
| 412 | # Only one item in the input sequence ? |
| 413 | if (len(seqarrays) == 1): |
| 414 | seqarrays = np.asanyarray(seqarrays[0]) |
| 415 | # Do we have a single ndarray as input ? |
| 416 | if isinstance(seqarrays, (np.ndarray, np.void)): |
| 417 | seqdtype = seqarrays.dtype |
| 418 | # Make sure we have named fields |
| 419 | if seqdtype.names is None: |
| 420 | seqdtype = np.dtype([('', seqdtype)]) |
| 421 | if not flatten or _zip_dtype((seqarrays,), flatten=True) == seqdtype: |
searching dependent graphs…