@@ -646,20 +646,33 @@ def fill_zeros(result, x, y, name, fill):
646646 return result
647647
648648
649- def mask_zero_div_zero (x , y , result ):
649+ def mask_zero_div_zero (x , y , result , copy = False ):
650650 """
651651 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.
653653
654654 Parameters
655655 ----------
656656 x : ndarray
657657 y : ndarray
658658 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.
659662
660663 Returns
661664 -------
662665 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])
663676 """
664677 if is_scalar (y ):
665678 y = np .array (y )
@@ -673,7 +686,7 @@ def mask_zero_div_zero(x, y, result):
673686 posinf_mask = (zmask & (x > 0 )).ravel ()
674687
675688 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 ()
677690
678691 np .putmask (result , nan_mask , np .nan )
679692 np .putmask (result , posinf_mask , np .inf )
0 commit comments