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