36 | def _replace_nan(a, val): |
37 | """ |
38 | If `a` is of inexact type, make a copy of `a`, replace NaNs with |
39 | the `val` value, and return the copy together with a boolean mask |
40 | marking the locations where NaNs were present. If `a` is not of |
41 | inexact type, do nothing and return `a` together with a mask of None. |
42 | |
43 | Note that scalars will end up as array scalars, which is important |
44 | for using the result as the value of the out argument in some |
45 | operations. |
46 | |
47 | Parameters |
48 | ---------- |
49 | a : array-like |
50 | Input array. |
51 | val : float |
52 | NaN values are set to val before doing the operation. |
53 | |
54 | Returns |
55 | ------- |
56 | y : ndarray |
57 | If `a` is of inexact type, return a copy of `a` with the NaNs |
58 | replaced by the fill value, otherwise return `a`. |
59 | mask: {bool, None} |
60 | If `a` is of inexact type, return a boolean mask marking locations of |
61 | NaNs, otherwise return None. |
62 | |
63 | """ |
64 | a = np.array(a, subok=True, copy=True) |
65 | |
66 | if a.dtype == np.object_: |
67 | |
68 | mask = a != a |
69 | elif issubclass(a.dtype.type, np.inexact): |
70 | mask = np.isnan(a) |
71 | else: |
72 | mask = None |
73 | |
74 | if mask is not None: |
75 | np.copyto(a, val, where=mask) |
76 | |
77 | return a, mask |