Add new fields to an existing array. The names of the fields are given with the `names` arguments, the corresponding values with the `data` arguments. If a single field is appended, `names`, `data` and `dtypes` do not have to be lists but just values. Parameters ------
(base, names, data, dtypes=None,
fill_value=-1, usemask=True, asrecarray=False)
| 653 | |
| 654 | @array_function_dispatch(_append_fields_dispatcher) |
| 655 | def append_fields(base, names, data, dtypes=None, |
| 656 | fill_value=-1, usemask=True, asrecarray=False): |
| 657 | """ |
| 658 | Add new fields to an existing array. |
| 659 | |
| 660 | The names of the fields are given with the `names` arguments, |
| 661 | the corresponding values with the `data` arguments. |
| 662 | If a single field is appended, `names`, `data` and `dtypes` do not have |
| 663 | to be lists but just values. |
| 664 | |
| 665 | Parameters |
| 666 | ---------- |
| 667 | base : array |
| 668 | Input array to extend. |
| 669 | names : string, sequence |
| 670 | String or sequence of strings corresponding to the names |
| 671 | of the new fields. |
| 672 | data : array or sequence of arrays |
| 673 | Array or sequence of arrays storing the fields to add to the base. |
| 674 | dtypes : sequence of datatypes, optional |
| 675 | Datatype or sequence of datatypes. |
| 676 | If None, the datatypes are estimated from the `data`. |
| 677 | fill_value : {float}, optional |
| 678 | Filling value used to pad missing data on the shorter arrays. |
| 679 | usemask : {False, True}, optional |
| 680 | Whether to return a masked array or not. |
| 681 | asrecarray : {False, True}, optional |
| 682 | Whether to return a recarray (MaskedRecords) or not. |
| 683 | |
| 684 | """ |
| 685 | # Check the names |
| 686 | if isinstance(names, (tuple, list)): |
| 687 | if len(names) != len(data): |
| 688 | msg = "The number of arrays does not match the number of names" |
| 689 | raise ValueError(msg) |
| 690 | elif isinstance(names, str): |
| 691 | names = [names, ] |
| 692 | data = [data, ] |
| 693 | # |
| 694 | if dtypes is None: |
| 695 | data = [np.array(a, copy=None, subok=True) for a in data] |
| 696 | data = [a.view([(name, a.dtype)]) for (name, a) in zip(names, data)] |
| 697 | else: |
| 698 | if not isinstance(dtypes, (tuple, list)): |
| 699 | dtypes = [dtypes, ] |
| 700 | if len(data) != len(dtypes): |
| 701 | if len(dtypes) == 1: |
| 702 | dtypes = dtypes * len(data) |
| 703 | else: |
| 704 | msg = "The dtypes argument must be None, a dtype, or a list." |
| 705 | raise ValueError(msg) |
| 706 | data = [np.array(a, copy=None, subok=True, dtype=d).view([(n, d)]) |
| 707 | for (a, n, d) in zip(data, names, dtypes)] |
| 708 | # |
| 709 | base = merge_arrays(base, usemask=usemask, fill_value=fill_value) |
| 710 | if len(data) > 1: |
| 711 | data = merge_arrays(data, flatten=True, usemask=usemask, |
| 712 | fill_value=fill_value) |
searching dependent graphs…