MCPcopy Create free account
hub / github.com/numpy/numpy / ndenumerate

Function ndenumerate

numpy/ma/extras.py:1665–1730  ·  view source on GitHub ↗

Multidimensional index iterator. Return an iterator yielding pairs of array coordinates and values, skipping elements that are masked. With `compressed=False`, `ma.masked` is yielded as the value of masked elements. This behavior differs from that of `numpy.ndenumerate`, which

(a, compressed=True)

Source from the content-addressed store, hash-verified

1663#####--------------------------------------------------------------------------
1664
1665def ndenumerate(a, compressed=True):
1666 """
1667 Multidimensional index iterator.
1668
1669 Return an iterator yielding pairs of array coordinates and values,
1670 skipping elements that are masked. With `compressed=False`,
1671 `ma.masked` is yielded as the value of masked elements. This
1672 behavior differs from that of `numpy.ndenumerate`, which yields the
1673 value of the underlying data array.
1674
1675 Notes
1676 -----
1677 .. versionadded:: 1.23.0
1678
1679 Parameters
1680 ----------
1681 a : array_like
1682 An array with (possibly) masked elements.
1683 compressed : bool, optional
1684 If True (default), masked elements are skipped.
1685
1686 See Also
1687 --------
1688 numpy.ndenumerate : Equivalent function ignoring any mask.
1689
1690 Examples
1691 --------
1692 >>> a = np.ma.arange(9).reshape((3, 3))
1693 >>> a[1, 0] = np.ma.masked
1694 >>> a[1, 2] = np.ma.masked
1695 >>> a[2, 1] = np.ma.masked
1696 >>> a
1697 masked_array(
1698 data=[[0, 1, 2],
1699 [--, 4, --],
1700 [6, --, 8]],
1701 mask=[[False, False, False],
1702 [ True, False, True],
1703 [False, True, False]],
1704 fill_value=999999)
1705 >>> for index, x in np.ma.ndenumerate(a):
1706 ... print(index, x)
1707 (0, 0) 0
1708 (0, 1) 1
1709 (0, 2) 2
1710 (1, 1) 4
1711 (2, 0) 6
1712 (2, 2) 8
1713
1714 >>> for index, x in np.ma.ndenumerate(a, compressed=False):
1715 ... print(index, x)
1716 (0, 0) 0
1717 (0, 1) 1
1718 (0, 2) 2
1719 (1, 0) --
1720 (1, 1) 4
1721 (1, 2) --
1722 (2, 0) 6

Calls 1

getmaskarrayFunction · 0.85

Tested by 3