10
10
- `nanargmax` -- index of maximum non-NaN value
11
11
- `nansum` -- sum of non-NaN values
12
12
- `nanprod` -- product of non-NaN values
13
+ - `nancumsum` -- cumulative sum of non-NaN values
14
+ - `nancumprod` -- cumulative product of non-NaN values
13
15
- `nanmean` -- mean of non-NaN values
14
16
- `nanvar` -- variance of non-NaN values
15
17
- `nanstd` -- standard deviation of non-NaN values
27
29
__all__ = [
28
30
'nansum' , 'nanmax' , 'nanmin' , 'nanargmax' , 'nanargmin' , 'nanmean' ,
29
31
'nanmedian' , 'nanpercentile' , 'nanvar' , 'nanstd' , 'nanprod' ,
32
+ 'nancumsum' , 'nancumprod'
30
33
]
31
34
32
35
@@ -493,7 +496,11 @@ def nansum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
493
496
494
497
Returns
495
498
-------
496
- y : ndarray or numpy scalar
499
+ nansum : ndarray.
500
+ A new array holding the result is returned unless `out` is
501
+ specified, in which it is returned. The result has the same
502
+ size as `a`, and the same shape as `a` if `axis` is not None
503
+ or `a` is a 1-d array.
497
504
498
505
See Also
499
506
--------
@@ -506,11 +513,6 @@ def nansum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
506
513
If both positive and negative infinity are present, the sum will be Not
507
514
A Number (NaN).
508
515
509
- Numpy integer arithmetic is modular. If the size of a sum exceeds the
510
- size of an integer accumulator, its value will wrap around and the
511
- result will be incorrect. Specifying ``dtype=double`` can alleviate
512
- that problem.
513
-
514
516
Examples
515
517
--------
516
518
>>> np.nansum(1)
@@ -539,7 +541,7 @@ def nansum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
539
541
def nanprod (a , axis = None , dtype = None , out = None , keepdims = np ._NoValue ):
540
542
"""
541
543
Return the product of array elements over a given axis treating Not a
542
- Numbers (NaNs) as zero .
544
+ Numbers (NaNs) as ones .
543
545
544
546
One is returned for slices that are all-NaN or empty.
545
547
@@ -573,20 +575,15 @@ def nanprod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
573
575
574
576
Returns
575
577
-------
576
- y : ndarray or numpy scalar
578
+ nanprod : ndarray
579
+ A new array holding the result is returned unless `out` is
580
+ specified, in which case it is returned.
577
581
578
582
See Also
579
583
--------
580
584
numpy.prod : Product across array propagating NaNs.
581
585
isnan : Show which elements are NaN.
582
586
583
- Notes
584
- -----
585
- Numpy integer arithmetic is modular. If the size of a product exceeds
586
- the size of an integer accumulator, its value will wrap around and the
587
- result will be incorrect. Specifying ``dtype=double`` can alleviate
588
- that problem.
589
-
590
587
Examples
591
588
--------
592
589
>>> np.nanprod(1)
@@ -606,6 +603,133 @@ def nanprod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
606
603
return np .prod (a , axis = axis , dtype = dtype , out = out , keepdims = keepdims )
607
604
608
605
606
+ def nancumsum (a , axis = None , dtype = None , out = None ):
607
+ """
608
+ Return the cumulative sum of array elements over a given axis treating Not a
609
+ Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are
610
+ encountered and leading NaNs are replaced by zeros.
611
+
612
+ Zeros are returned for slices that are all-NaN or empty.
613
+
614
+ .. versionadded:: 1.12.0
615
+
616
+ Parameters
617
+ ----------
618
+ a : array_like
619
+ Input array.
620
+ axis : int, optional
621
+ Axis along which the cumulative sum is computed. The default
622
+ (None) is to compute the cumsum over the flattened array.
623
+ dtype : dtype, optional
624
+ Type of the returned array and of the accumulator in which the
625
+ elements are summed. If `dtype` is not specified, it defaults
626
+ to the dtype of `a`, unless `a` has an integer dtype with a
627
+ precision less than that of the default platform integer. In
628
+ that case, the default platform integer is used.
629
+ out : ndarray, optional
630
+ Alternative output array in which to place the result. It must
631
+ have the same shape and buffer leng
10000
th as the expected output
632
+ but the type will be cast if necessary. See `doc.ufuncs`
633
+ (Section "Output arguments") for more details.
634
+
635
+ Returns
636
+ -------
637
+ nancumsum : ndarray.
638
+ A new array holding the result is returned unless `out` is
639
+ specified, in which it is returned. The result has the same
640
+ size as `a`, and the same shape as `a` if `axis` is not None
641
+ or `a` is a 1-d array.
642
+
643
+ See Also
644
+ --------
645
+ numpy.cumsum : Cumulative sum across array propagating NaNs.
646
+ isnan : Show which elements are NaN.
647
+
648
+ Examples
649
+ --------
650
+ >>> np.nancumsum(1)
651
+ array([1])
652
+ >>> np.nancumsum([1])
653
+ array([1])
654
+ >>> np.nancumsum([1, np.nan])
655
+ array([ 1., 1.])
656
+ >>> a = np.array([[1, 2], [3, np.nan]])
657
+ >>> np.nancumsum(a)
658
+ array([ 1., 3., 6., 6.])
659
+ >>> np.nancumsum(a, axis=0)
660
+ array([[ 1., 2.],
661
+ [ 4., 2.]])
662
+ >>> np.nancumsum(a, axis=1)
663
+ array([[ 1., 3.],
664
+ [ 3., 3.]])
665
+
666
+ """
667
+ a , mask = _replace_nan (a , 0 )
668
+ return np .cumsum (a , axis = axis , dtype = dtype , out = out )
669
+
670
+
671
+ def nancumprod (a , axis = None , dtype = None , out = None ):
672
+ """
673
+ Return the cumulative product of array elements over a given axis treating Not a
674
+ Numbers (NaNs) as one. The cumulative product does not change when NaNs are
675
+ encountered and leading NaNs are replaced by ones.
676
+
677
+ Ones are returned for slices that are all-NaN or empty.
678
+
679
+ .. versionadded:: 1.12.0
680
+
681
+ Parameters
682
+ ----------
683
+ a : array_like
684
+ Input array.
685
+ axis : int, optional
686
+ Axis along which the cumulative product is computed. By default
687
+ the input is flattened.
688
+ dtype : dtype, optional
689
+ Type of the returned array, as well as of the accumulator in which
690
+ the elements are multiplied. If *dtype* is not specified, it
691
+ defaults to the dtype of `a`, unless `a` has an integer dtype with
692
+ a precision less than that of the default platform integer. In
693
+ that case, the default platform integer is used instead.
694
+ out : ndarray, optional
695
+ Alternative output array in which to place the result. It must
696
+ have the same shape and buffer length as the expected output
697
+ but the type of the resulting values will be cast if necessary.
698
+
699
+ Returns
700
+ -------
701
+ nancumprod : ndarray
702
+ A new array holding the result is returned unless `out` is
703
+ specified, in which case it is returned.
704
+
705
+ See Also
706
+ --------
707
+ numpy.cumprod : Cumulative product across array propagating NaNs.
708
+ isnan : Show which elements are NaN.
709
+
710
+ Examples
711
+ --------
712
+ >>> np.nancumprod(1)
713
+ array([1])
714
+ >>> np.nancumprod([1])
715
+ array([1])
716
+ >>> np.nancumprod([1, np.nan])
717
+ array([ 1., 1.])
718
+ >>> a = np.array([[1, 2], [3, np.nan]])
719
+ >>> np.nancumprod(a)
720
+ array([ 1., 2., 6., 6.])
721
+ >>> np.nancumprod(a, axis=0)
722
+ array([[ 1., 2.],
723
+ [ 3., 2.]])
724
+ >>> np.nancumprod(a, axis=1)
725
+ array([[ 1., 2.],
726
+ [ 3., 3.]])
727
+
728
+ """
729
+ a , mask = _replace_nan (a , 1 )
730
+ return np .cumprod (a , axis = axis , dtype = dtype , out = out )
731
+
732
+
609
733
def nanmean (a , axis = None , dtype = None , out = None , keepdims = np ._NoValue ):
610
734
"""
611
735
Compute the arithmetic mean along the specified axis, ignoring NaNs.
0 commit comments