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

Function fix_invalid

numpy/ma/core.py:725–781  ·  view source on GitHub ↗

Return input with invalid data masked and replaced by a fill value. Invalid data means values of `nan`, `inf`, etc. Parameters ---------- a : array_like Input array, a (subclass of) ndarray. mask : sequence, optional Mask. Must be convertible to an array of

(a, mask=nomask, copy=True, fill_value=None)

Source from the content-addressed store, hash-verified

723
724
725def fix_invalid(a, mask=nomask, copy=True, fill_value=None):
726 """
727 Return input with invalid data masked and replaced by a fill value.
728
729 Invalid data means values of `nan`, `inf`, etc.
730
731 Parameters
732 ----------
733 a : array_like
734 Input array, a (subclass of) ndarray.
735 mask : sequence, optional
736 Mask. Must be convertible to an array of booleans with the same
737 shape as `data`. True indicates a masked (i.e. invalid) data.
738 copy : bool, optional
739 Whether to use a copy of `a` (True) or to fix `a` in place (False).
740 Default is True.
741 fill_value : scalar, optional
742 Value used for fixing invalid data. Default is None, in which case
743 the ``a.fill_value`` is used.
744
745 Returns
746 -------
747 b : MaskedArray
748 The input array with invalid entries fixed.
749
750 Notes
751 -----
752 A copy is performed by default.
753
754 Examples
755 --------
756 >>> x = np.ma.array([1., -1, np.nan, np.inf], mask=[1] + [0]*3)
757 >>> x
758 masked_array(data=[--, -1.0, nan, inf],
759 mask=[ True, False, False, False],
760 fill_value=1e+20)
761 >>> np.ma.fix_invalid(x)
762 masked_array(data=[--, -1.0, --, --],
763 mask=[ True, False, True, True],
764 fill_value=1e+20)
765
766 >>> fixed = np.ma.fix_invalid(x)
767 >>> fixed.data
768 array([ 1.e+00, -1.e+00, 1.e+20, 1.e+20])
769 >>> x.data
770 array([ 1., -1., nan, inf])
771
772 """
773 a = masked_array(a, copy=copy, mask=mask, subok=True)
774 invalid = np.logical_not(np.isfinite(a._data))
775 if not invalid.any():
776 return a
777 a._mask |= invalid
778 if fill_value is None:
779 fill_value = a.fill_value
780 a._data[invalid] = fill_value
781 return a
782

Callers 1

test_fix_invalidMethod · 0.90

Calls 1

anyMethod · 0.45

Tested by 1

test_fix_invalidMethod · 0.72