summaryrefslogtreecommitdiff
path: root/numpy/ma/extras.py
diff options
context:
space:
mode:
authorJouke Witteveen <j.witteveen@gmail.com>2021-10-02 13:05:08 +0200
committerJouke Witteveen <j.witteveen@gmail.com>2022-04-22 11:49:16 +0200
commit701f0a603c63d1176e6e0d9cf775873f1585c2e2 (patch)
tree3ffa6fd827a4f85e8a8e247df52830fb12074384 /numpy/ma/extras.py
parent79a5000a72f6e4432c0a76efa6cf8e3f12ecef9e (diff)
downloadnumpy-701f0a603c63d1176e6e0d9cf775873f1585c2e2.tar.gz
ENH: add ndenumerate specialization for masked arrays
Diffstat (limited to 'numpy/ma/extras.py')
-rw-r--r--numpy/ma/extras.py57
1 files changed, 51 insertions, 6 deletions
diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py
index 048d94bb7..de20714d8 100644
--- a/numpy/ma/extras.py
+++ b/numpy/ma/extras.py
@@ -10,12 +10,12 @@ A collection of utilities for `numpy.ma`.
"""
__all__ = [
'apply_along_axis', 'apply_over_axes', 'atleast_1d', 'atleast_2d',
- 'atleast_3d', 'average', 'clump_masked', 'clump_unmasked',
- 'column_stack', 'compress_cols', 'compress_nd', 'compress_rowcols',
- 'compress_rows', 'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot',
- 'dstack', 'ediff1d', 'flatnotmasked_contiguous', 'flatnotmasked_edges',
- 'hsplit', 'hstack', 'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols',
- 'mask_rows', 'masked_all', 'masked_all_like', 'median', 'mr_',
+ 'atleast_3d', 'average', 'clump_masked', 'clump_unmasked', 'column_stack',
+ 'compress_cols', 'compress_nd', 'compress_rowcols', 'compress_rows',
+ 'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot', 'dstack', 'ediff1d',
+ 'flatnotmasked_contiguous', 'flatnotmasked_edges', 'hsplit', 'hstack',
+ 'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols', 'mask_rows',
+ 'masked_all', 'masked_all_like', 'median', 'mr_', 'ndenumerate',
'notmasked_contiguous', 'notmasked_edges', 'polyfit', 'row_stack',
'setdiff1d', 'setxor1d', 'stack', 'unique', 'union1d', 'vander', 'vstack',
]
@@ -1520,6 +1520,51 @@ mr_ = mr_class()
#---- Find unmasked data ---
#####--------------------------------------------------------------------------
+def ndenumerate(a):
+ """
+ Multidimensional index iterator.
+
+ Return an iterator yielding pairs of array coordinates and values of
+ elements that are not masked.
+
+ Parameters
+ ----------
+ a : array_like
+ An array with (possibly) masked elements.
+
+ See Also
+ --------
+ numpy.ndenumerate : Equivalent function ignoring any mask.
+
+ Examples
+ --------
+ >>> a = np.ma.arange(9).reshape((3, 3))
+ >>> a[1, 0] = np.ma.masked
+ >>> a[1, 2] = np.ma.masked
+ >>> a[2, 1] = np.ma.masked
+ >>> a
+ masked_array(
+ data=[[0, 1, 2],
+ [--, 4, --],
+ [6, --, 8]],
+ mask=[[False, False, False],
+ [ True, False, True],
+ [False, True, False]],
+ fill_value=999999)
+ >>> for index, x in np.ma.ndenumerate(a):
+ ... print(index, x)
+ (0, 0) 0
+ (0, 1) 1
+ (0, 2) 2
+ (1, 1) 4
+ (2, 0) 6
+ (2, 2) 8
+ """
+ for it, masked in zip(np.ndenumerate(a), getmaskarray(a).flat):
+ if not masked:
+ yield it
+
+
def flatnotmasked_edges(a):
"""
Find the indices of the first and last unmasked values.