Mask an array where invalid values occur (NaNs or infs). This function is a shortcut to ``masked_where``, with `condition` = ~(np.isfinite(a)). Any pre-existing mask is conserved. Only applies to arrays with a dtype where NaNs or infs make sense (i.e. floating point types), but
(a, copy=True)
| 2398 | |
| 2399 | |
| 2400 | def masked_invalid(a, copy=True): |
| 2401 | """ |
| 2402 | Mask an array where invalid values occur (NaNs or infs). |
| 2403 | |
| 2404 | This function is a shortcut to ``masked_where``, with |
| 2405 | `condition` = ~(np.isfinite(a)). Any pre-existing mask is conserved. |
| 2406 | Only applies to arrays with a dtype where NaNs or infs make sense |
| 2407 | (i.e. floating point types), but accepts any array_like object. |
| 2408 | |
| 2409 | See Also |
| 2410 | -------- |
| 2411 | masked_where : Mask where a condition is met. |
| 2412 | |
| 2413 | Examples |
| 2414 | -------- |
| 2415 | >>> import numpy as np |
| 2416 | >>> import numpy.ma as ma |
| 2417 | >>> a = np.arange(5, dtype=np.float64) |
| 2418 | >>> a[2] = np.nan |
| 2419 | >>> a[3] = np.inf |
| 2420 | >>> a |
| 2421 | array([ 0., 1., nan, inf, 4.]) |
| 2422 | >>> ma.masked_invalid(a) |
| 2423 | masked_array(data=[0.0, 1.0, --, --, 4.0], |
| 2424 | mask=[False, False, True, True, False], |
| 2425 | fill_value=1e+20) |
| 2426 | |
| 2427 | """ |
| 2428 | a = np.array(a, copy=None, subok=True) |
| 2429 | res = masked_where(~(np.isfinite(a)), a, copy=copy) |
| 2430 | # masked_invalid previously never returned nomask as a mask and doing so |
| 2431 | # threw off matplotlib (gh-22842). So use shrink=False: |
| 2432 | if res._mask is nomask: |
| 2433 | res._mask = make_mask_none(res.shape, res.dtype) |
| 2434 | return res |
| 2435 | |
| 2436 | ############################################################################### |
| 2437 | # Printing options # |
nothing calls this directly
no test coverage detected
searching dependent graphs…