If `a` is of inexact type, make a copy of `a`, replace NaNs with the `val` value, and return the copy together with a boolean mask marking the locations where NaNs were present. If `a` is not of inexact type, do nothing and return `a` together with a mask of None. Note that sca
(a, val)
| 66 | return y |
| 67 | |
| 68 | def _replace_nan(a, val): |
| 69 | """ |
| 70 | If `a` is of inexact type, make a copy of `a`, replace NaNs with |
| 71 | the `val` value, and return the copy together with a boolean mask |
| 72 | marking the locations where NaNs were present. If `a` is not of |
| 73 | inexact type, do nothing and return `a` together with a mask of None. |
| 74 | |
| 75 | Note that scalars will end up as array scalars, which is important |
| 76 | for using the result as the value of the out argument in some |
| 77 | operations. |
| 78 | |
| 79 | Parameters |
| 80 | ---------- |
| 81 | a : array-like |
| 82 | Input array. |
| 83 | val : float |
| 84 | NaN values are set to val before doing the operation. |
| 85 | |
| 86 | Returns |
| 87 | ------- |
| 88 | y : ndarray |
| 89 | If `a` is of inexact type, return a copy of `a` with the NaNs |
| 90 | replaced by the fill value, otherwise return `a`. |
| 91 | mask: {bool, None} |
| 92 | If `a` is of inexact type, return a boolean mask marking locations of |
| 93 | NaNs, otherwise return None. |
| 94 | |
| 95 | """ |
| 96 | a = np.asanyarray(a) |
| 97 | |
| 98 | if a.dtype == np.object_: |
| 99 | # object arrays do not support `isnan` (gh-9009), so make a guess |
| 100 | mask = np.not_equal(a, a, dtype=bool) |
| 101 | elif issubclass(a.dtype.type, np.inexact): |
| 102 | mask = np.isnan(a) |
| 103 | else: |
| 104 | mask = None |
| 105 | |
| 106 | if mask is not None: |
| 107 | a = np.array(a, subok=True, copy=True) |
| 108 | np.copyto(a, val, where=mask) |
| 109 | |
| 110 | return a, mask |
| 111 | |
| 112 | |
| 113 | def _copyto(a, val, mask): |
no outgoing calls