Return a new array with fields in `drop_names` dropped. Nested fields are supported. .. versionchanged:: 1.18.0 `drop_fields` returns an array with 0 fields if all fields are dropped, rather than returning ``None`` as it did previously. Parameters ----------
(base, drop_names, usemask=True, asrecarray=False)
| 500 | |
| 501 | @array_function_dispatch(_drop_fields_dispatcher) |
| 502 | def drop_fields(base, drop_names, usemask=True, asrecarray=False): |
| 503 | """ |
| 504 | Return a new array with fields in `drop_names` dropped. |
| 505 | |
| 506 | Nested fields are supported. |
| 507 | |
| 508 | .. versionchanged:: 1.18.0 |
| 509 | `drop_fields` returns an array with 0 fields if all fields are dropped, |
| 510 | rather than returning ``None`` as it did previously. |
| 511 | |
| 512 | Parameters |
| 513 | ---------- |
| 514 | base : array |
| 515 | Input array |
| 516 | drop_names : string or sequence |
| 517 | String or sequence of strings corresponding to the names of the |
| 518 | fields to drop. |
| 519 | usemask : {False, True}, optional |
| 520 | Whether to return a masked array or not. |
| 521 | asrecarray : string or sequence, optional |
| 522 | Whether to return a recarray or a mrecarray (`asrecarray=True`) or |
| 523 | a plain ndarray or masked array with flexible dtype. The default |
| 524 | is False. |
| 525 | |
| 526 | Examples |
| 527 | -------- |
| 528 | >>> from numpy.lib import recfunctions as rfn |
| 529 | >>> a = np.array([(1, (2, 3.0)), (4, (5, 6.0))], |
| 530 | ... dtype=[('a', np.int64), ('b', [('ba', np.double), ('bb', np.int64)])]) |
| 531 | >>> rfn.drop_fields(a, 'a') |
| 532 | array([((2., 3),), ((5., 6),)], |
| 533 | dtype=[('b', [('ba', '<f8'), ('bb', '<i8')])]) |
| 534 | >>> rfn.drop_fields(a, 'ba') |
| 535 | array([(1, (3,)), (4, (6,))], dtype=[('a', '<i8'), ('b', [('bb', '<i8')])]) |
| 536 | >>> rfn.drop_fields(a, ['ba', 'bb']) |
| 537 | array([(1,), (4,)], dtype=[('a', '<i8')]) |
| 538 | """ |
| 539 | if _is_string_like(drop_names): |
| 540 | drop_names = [drop_names] |
| 541 | else: |
| 542 | drop_names = set(drop_names) |
| 543 | |
| 544 | def _drop_descr(ndtype, drop_names): |
| 545 | names = ndtype.names |
| 546 | newdtype = [] |
| 547 | for name in names: |
| 548 | current = ndtype[name] |
| 549 | if name in drop_names: |
| 550 | continue |
| 551 | if current.names is not None: |
| 552 | descr = _drop_descr(current, drop_names) |
| 553 | if descr: |
| 554 | newdtype.append((name, descr)) |
| 555 | else: |
| 556 | newdtype.append((name, current)) |
| 557 | return newdtype |
| 558 | |
| 559 | newdtype = _drop_descr(base.dtype, drop_names) |