@@ -646,20 +646,33 @@ def fill_zeros(result, x, y, name, fill):
646
646
return result
647
647
648
648
649
- def mask_zero_div_zero (x , y , result ):
649
+ def mask_zero_div_zero (x , y , result , copy = False ):
650
650
"""
651
651
Set results of 0 / 0 or 0 // 0 to np.nan, regardless of the dtypes
652
- of the numerator or the denominator
652
+ of the numerator or the denominator.
653
653
654
654
Parameters
655
655
----------
656
656
x : ndarray
657
657
y : ndarray
658
658
result : ndarray
659
+ copy : bool (default False)
660
+ Whether to always create a new array or try to fill in the existing
661
+ array if possible.
659
662
660
663
Returns
661
664
-------
662
665
filled_result : ndarray
666
+
667
+ Examples
668
+ --------
669
+ >>> x = np.array([1, 0, -1], dtype=np.int64)
670
+ >>> y = 0 # int 0; numpy behavior is different with float
671
+ >>> result = x / y
672
+ >>> result # raw numpy result does not fill division by zero
673
+ array([0, 0, 0])
674
+ >>> mask_zero_div_zero(x, y, result)
675
+ array([ inf, nan, -inf])
663
676
"""
664
677
if is_scalar (y ):
665
678
y = np .array (y )
@@ -673,7 +686,7 @@ def mask_zero_div_zero(x, y, result):
673
686
posinf_mask = (zmask & (x > 0 )).ravel ()
674
687
675
688
if nan_mask .any () or neginf_mask .any () or posinf_mask .any ():
676
- result = result .astype ('float64' , copy = False ).ravel ()
689
+ result = result .astype ('float64' , copy = copy ).ravel ()
677
690
678
691
np .putmask (result , nan_mask , np .nan )
679
692
np .putmask (result , posinf_mask , np .inf )
0 commit comments