MCPcopy Create free account
hub / github.com/numpy/numpy / _remove_nan_1d

Function _remove_nan_1d

numpy/lib/nanfunctions.py:142–184  ·  view source on GitHub ↗

Equivalent to arr1d[~arr1d.isnan()], but in a different order Presumably faster as it incurs fewer copies Parameters ---------- arr1d : ndarray Array to remove nans from overwrite_input : bool True if `arr1d` can be modified in place Returns ------

(arr1d, overwrite_input=False)

Source from the content-addressed store, hash-verified

140
141
142def _remove_nan_1d(arr1d, overwrite_input=False):
143 """
144 Equivalent to arr1d[~arr1d.isnan()], but in a different order
145
146 Presumably faster as it incurs fewer copies
147
148 Parameters
149 ----------
150 arr1d : ndarray
151 Array to remove nans from
152 overwrite_input : bool
153 True if `arr1d` can be modified in place
154
155 Returns
156 -------
157 res : ndarray
158 Array with nan elements removed
159 overwrite_input : bool
160 True if `res` can be modified in place, given the constraint on the
161 input
162 """
163 if arr1d.dtype == object:
164 # object arrays do not support `isnan` (gh-9009), so make a guess
165 c = np.not_equal(arr1d, arr1d, dtype=bool)
166 else:
167 c = np.isnan(arr1d)
168
169 s = np.nonzero(c)[0]
170 if s.size == arr1d.size:
171 warnings.warn("All-NaN slice encountered", RuntimeWarning,
172 stacklevel=6)
173 return arr1d[:0], True
174 elif s.size == 0:
175 return arr1d, overwrite_input
176 else:
177 if not overwrite_input:
178 arr1d = arr1d.copy()
179 # select non-nans at end of array
180 enonan = arr1d[-s.size:][~c[-s.size:]]
181 # fill nans in beginning of array with non-nans of end
182 arr1d[s[:enonan.size]] = enonan
183
184 return arr1d[:-s.size], True
185
186
187def _divide_by_count(a, b, out=None):

Callers 2

_nanmedian1dFunction · 0.85
_nanquantile_1dFunction · 0.85

Calls 3

nonzeroMethod · 0.80
warnMethod · 0.80
copyMethod · 0.45

Tested by

no test coverage detected