Return a copy of self, with masked values filled with a given value. **However**, if there are no masked values to fill, self will be returned instead as an ndarray. Parameters ---------- fill_value : array_like, optional The value to use
(self, fill_value=None)
| 3855 | set_fill_value = fill_value.fset |
| 3856 | |
| 3857 | def filled(self, fill_value=None): |
| 3858 | """ |
| 3859 | Return a copy of self, with masked values filled with a given value. |
| 3860 | **However**, if there are no masked values to fill, self will be |
| 3861 | returned instead as an ndarray. |
| 3862 | |
| 3863 | Parameters |
| 3864 | ---------- |
| 3865 | fill_value : array_like, optional |
| 3866 | The value to use for invalid entries. Can be scalar or non-scalar. |
| 3867 | If non-scalar, the resulting ndarray must be broadcastable over |
| 3868 | input array. Default is None, in which case, the `fill_value` |
| 3869 | attribute of the array is used instead. |
| 3870 | |
| 3871 | Returns |
| 3872 | ------- |
| 3873 | filled_array : ndarray |
| 3874 | A copy of ``self`` with invalid entries replaced by *fill_value* |
| 3875 | (be it the function argument or the attribute of ``self``), or |
| 3876 | ``self`` itself as an ndarray if there are no invalid entries to |
| 3877 | be replaced. |
| 3878 | |
| 3879 | Notes |
| 3880 | ----- |
| 3881 | The result is **not** a MaskedArray! |
| 3882 | |
| 3883 | Examples |
| 3884 | -------- |
| 3885 | >>> import numpy as np |
| 3886 | >>> x = np.ma.array([1,2,3,4,5], mask=[0,0,1,0,1], fill_value=-999) |
| 3887 | >>> x.filled() |
| 3888 | array([ 1, 2, -999, 4, -999]) |
| 3889 | >>> x.filled(fill_value=1000) |
| 3890 | array([ 1, 2, 1000, 4, 1000]) |
| 3891 | >>> type(x.filled()) |
| 3892 | <class 'numpy.ndarray'> |
| 3893 | |
| 3894 | Subclassing is preserved. This means that if, e.g., the data part of |
| 3895 | the masked array is a recarray, `filled` returns a recarray: |
| 3896 | |
| 3897 | >>> x = np.array([(-1, 2), (-3, 4)], dtype='i8,i8').view(np.recarray) |
| 3898 | >>> m = np.ma.array(x, mask=[(True, False), (False, True)]) |
| 3899 | >>> m.filled() |
| 3900 | rec.array([(999999, 2), ( -3, 999999)], |
| 3901 | dtype=[('f0', '<i8'), ('f1', '<i8')]) |
| 3902 | """ |
| 3903 | m = self._mask |
| 3904 | if m is nomask: |
| 3905 | return self._data |
| 3906 | |
| 3907 | if fill_value is None: |
| 3908 | fill_value = self.fill_value |
| 3909 | else: |
| 3910 | fill_value = _check_fill_value(fill_value, self.dtype) |
| 3911 | |
| 3912 | if self is masked_singleton: |
| 3913 | return np.asanyarray(fill_value) |
| 3914 |