diff options
author | Mark Wiebe <mwwiebe@gmail.com> | 2011-08-17 19:18:56 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2011-08-27 07:26:56 -0600 |
commit | bfda229ec93d37b1ee2cdd8b9443ec4e34536bbf (patch) | |
tree | 1315cce82b32c73eb9390e3fa46d8a8e0ddecebe /numpy/add_newdocs.py | |
parent | d9b3f90de3213ece9a78b77088fdec17910e81d9 (diff) | |
download | numpy-bfda229ec93d37b1ee2cdd8b9443ec4e34536bbf.tar.gz |
ENH: missingdata: Create count_reduce_items function
This function either cheaply returns the product of the sizes of
all the reduction axes, or counts the number of items which will
be used in a reduction operation when skipna is True. Its purpose
is to make it easy to do functions like np.mean and np.std.
Diffstat (limited to 'numpy/add_newdocs.py')
-rw-r--r-- | numpy/add_newdocs.py | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py index 82f59c6b0..8596b9c9c 100644 --- a/numpy/add_newdocs.py +++ b/numpy/add_newdocs.py @@ -915,6 +915,56 @@ add_newdoc('numpy.core.multiarray', 'count_nonzero', 5 """) +add_newdoc('numpy.core.multiarray', 'count_reduce_items', + """ + count_reduce_items(arr, axis=None, skipna=False) + + Counts the number of items a reduction with the same `axis` + and `skipna` parameter values would use. The purpose of this + function is for the creation of reduction operations + which use the item count, such as :func:`mean`. + + When `skipna` is False or `arr` doesn't have an NA mask, + the result is simply the product of the reduction axis + sizes, returned as a single scalar. + + Parameters + ---------- + arr : array_like + The array for which to count the reduce items. + axis : None or int or tuple of ints, optional + Axis or axes along which a reduction is performed. + The default (`axis` = None) is perform a reduction over all + the dimensions of the input array. + skipna : bool, optional + If this is set to True, any NA elements in the array are not + counted. The only time this function does any actual counting + instead of a cheap multiply of a few sizes is when `skipna` is + true and `arr` has an NA mask. + + Returns + ------- + count : intp or array of intp + Number of items that would be used in a reduction with the + same `axis` and `skipna` parameter values. + + Examples + -------- + >>> a = np.array([[1,np.NA,1], [1,1,np.NA]]) + + >>> np.count_reduce_items(a) + 6 + >>> np.count_reduce_items(a, skipna=True) + 4 + >>> np.sum(a, skipna=True) + 4 + + >>> np.count_reduce_items(a, axis=0, skipna=True) + array([2, 1, 1]) + >>> np.sum(a, axis=0, skipna=True) + array([2, 1, 1]) + """) + add_newdoc('numpy.core.multiarray','set_typeDict', """set_typeDict(dict) @@ -5280,7 +5330,7 @@ add_newdoc('numpy.core', 'ufunc', ('types', add_newdoc('numpy.core', 'ufunc', ('reduce', """ - reduce(a, axis=0, dtype=None, out=None) + reduce(a, axis=0, dtype=None, out=None, skipna=False) Reduces `a`'s dimension by one, by applying ufunc along one axis. @@ -5325,6 +5375,11 @@ add_newdoc('numpy.core', 'ufunc', ('reduce', out : ndarray, optional A location into which the result is stored. If not provided, a freshly-allocated array is returned. + skipna : bool, optional + If this is set to True, the reduction is done as if any NA elements + were not counted in the array. The default, False, causes the + NA values to propagate, so if any element in a set of elements + being reduced is NA, the result will be NA. Returns ------- |