|
10 | 10 | """
|
11 | 11 | __all__ = [
|
12 | 12 | 'apply_along_axis', 'apply_over_axes', 'atleast_1d', 'atleast_2d',
|
13 |
| - 'atleast_3d', 'average', 'clump_masked', 'clump_unmasked', |
14 |
| - 'column_stack', 'compress_cols', 'compress_nd', 'compress_rowcols', |
15 |
| - 'compress_rows', 'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot', |
16 |
| - 'dstack', 'ediff1d', 'flatnotmasked_contiguous', 'flatnotmasked_edges', |
17 |
| - 'hsplit', 'hstack', 'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols', |
18 |
| - 'mask_rows', 'masked_all', 'masked_all_like', 'median', 'mr_', |
| 13 | + 'atleast_3d', 'average', 'clump_masked', 'clump_unmasked', 'column_stack', |
| 14 | + 'compress_cols', 'compress_nd', 'compress_rowcols', 'compress_rows', |
| 15 | + 'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot', 'dstack', 'ediff1d', |
| 16 | + 'flatnotmasked_contiguous', 'flatnotmasked_edges', 'hsplit', 'hstack', |
| 17 | + 'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols', 'mask_rows', |
| 18 | + 'masked_all', 'masked_all_like', 'median', 'mr_', 'ndenumerate', |
19 | 19 | 'notmasked_contiguous', 'notmasked_edges', 'polyfit', 'row_stack',
|
20 | 20 | 'setdiff1d', 'setxor1d', 'stack', 'unique', 'union1d', 'vander', 'vstack',
|
21 | 21 | ]
|
@@ -1552,6 +1552,74 @@ def __init__(self):
|
1552 | 1552 | #---- Find unmasked data ---
|
1553 | 1553 | #####--------------------------------------------------------------------------
|
1554 | 1554 |
|
| 1555 | +def ndenumerate(a, compressed=True): |
| 1556 | + """ |
| 1557 | + Multidimensional index iterator. |
| 1558 | +
|
| 1559 | + Return an iterator yielding pairs of array coordinates and values, |
| 1560 | + skipping elements that are masked. With `compressed=False`, |
| 1561 | + `ma.masked` is yielded as the value of masked elements. This |
| 1562 | + behavior differs from that of `numpy.ndenumerate`, which yields the |
| 1563 | + value of the underlying data array. |
| 1564 | +
|
| 1565 | + Notes |
| 1566 | + ----- |
| 1567 | + .. versionadded:: 1.23.0 |
| 1568 | +
|
| 1569 | + Parameters |
| 1570 | + ---------- |
| 1571 | + a : array_like |
| 1572 | + An array with (possibly) masked elements. |
| 1573 | + compressed : bool, optional |
| 1574 | + If True (default), masked elements are skipped. |
| 1575 | +
|
| 1576 | + See Also |
| 1577 | + -------- |
| 1578 | + numpy.ndenumerate : Equivalent function ignoring any mask. |
| 1579 | +
|
| 1580 | + Examples |
| 1581 | + -------- |
| 1582 | + >>> a = np.ma.arange(9).reshape((3, 3)) |
| 1583 | + >>> a[1, 0] = np.ma.masked |
| 1584 | + >>> a[1, 2] = np.ma.masked |
| 1585 | + >>> a[2, 1] = np.ma.masked |
| 1586 | + >>> a |
| 1587 | + masked_array( |
| 1588 | + data=[[0, 1, 2], |
| 1589 | + [--, 4, --], |
| 1590 | + [6, --, 8]], |
| 1591 | + mask=[[False, False, False], |
| 1592 | + [ True, False, True], |
| 1593 | + [False, True, False]], |
| 1594 | + fill_value=999999) |
| 1595 | + >>> for index, x in np.ma.ndenumerate(a): |
| 1596 | + ... print(index, x) |
| 1597 | + (0, 0) 0 |
| 1598 | + (0, 1) 1 |
| 1599 | + (0, 2) 2 |
| 1600 | + (1, 1) 4 |
| 1601 | + (2, 0) 6 |
| 1602 | + (2, 2) 8 |
| 1603 | +
|
| 1604 | + >>> for index, x in np.ma.ndenumerate(a, compressed=False): |
| 1605 | + ... print(index, x) |
| 1606 | + (0, 0) 0 |
| 1607 | + (0, 1) 1 |
| 1608 | + (0, 2) 2 |
| 1609 | + (1, 0) -- |
| 1610 | + (1, 1) 4 |
| 1611 | + (1, 2) -- |
| 1612 | + (2, 0) 6 |
| 1613 | + (2, 1) -- |
| 1614 | + (2, 2) 8 |
| 1615 | + """ |
| 1616 | + for it, mask in zip(np.ndenumerate(a), getmaskarray(a).flat): |
| 1617 | + if not mask: |
| 1618 | + yield it |
| 1619 | + elif not compressed: |
| 1620 | + yield it[0], masked |
| 1621 | + |
| 1622 | + |
1555 | 1623 | def flatnotmasked_edges(a):
|
1556 | 1624 | """
|
1557 | 1625 | Find the indices of the first and last unmasked values.
|
|
0 commit comments