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)
| 3784 | set_fill_value = fill_value.fset |
| 3785 | |
| 3786 | def filled(self, fill_value=None): |
| 3787 | """ |
| 3788 | Return a copy of self, with masked values filled with a given value. |
| 3789 | **However**, if there are no masked values to fill, self will be |
| 3790 | returned instead as an ndarray. |
| 3791 | |
| 3792 | Parameters |
| 3793 | ---------- |
| 3794 | fill_value : array_like, optional |
| 3795 | The value to use for invalid entries. Can be scalar or non-scalar. |
| 3796 | If non-scalar, the resulting ndarray must be broadcastable over |
| 3797 | input array. Default is None, in which case, the `fill_value` |
| 3798 | attribute of the array is used instead. |
| 3799 | |
| 3800 | Returns |
| 3801 | ------- |
| 3802 | filled_array : ndarray |
| 3803 | A copy of ``self`` with invalid entries replaced by *fill_value* |
| 3804 | (be it the function argument or the attribute of ``self``), or |
| 3805 | ``self`` itself as an ndarray if there are no invalid entries to |
| 3806 | be replaced. |
| 3807 | |
| 3808 | Notes |
| 3809 | ----- |
| 3810 | The result is **not** a MaskedArray! |
| 3811 | |
| 3812 | Examples |
| 3813 | -------- |
| 3814 | >>> x = np.ma.array([1,2,3,4,5], mask=[0,0,1,0,1], fill_value=-999) |
| 3815 | >>> x.filled() |
| 3816 | array([ 1, 2, -999, 4, -999]) |
| 3817 | >>> x.filled(fill_value=1000) |
| 3818 | array([ 1, 2, 1000, 4, 1000]) |
| 3819 | >>> type(x.filled()) |
| 3820 | <class 'numpy.ndarray'> |
| 3821 | |
| 3822 | Subclassing is preserved. This means that if, e.g., the data part of |
| 3823 | the masked array is a recarray, `filled` returns a recarray: |
| 3824 | |
| 3825 | >>> x = np.array([(-1, 2), (-3, 4)], dtype='i8,i8').view(np.recarray) |
| 3826 | >>> m = np.ma.array(x, mask=[(True, False), (False, True)]) |
| 3827 | >>> m.filled() |
| 3828 | rec.array([(999999, 2), ( -3, 999999)], |
| 3829 | dtype=[('f0', '<i8'), ('f1', '<i8')]) |
| 3830 | """ |
| 3831 | m = self._mask |
| 3832 | if m is nomask: |
| 3833 | return self._data |
| 3834 | |
| 3835 | if fill_value is None: |
| 3836 | fill_value = self.fill_value |
| 3837 | else: |
| 3838 | fill_value = _check_fill_value(fill_value, self.dtype) |
| 3839 | |
| 3840 | if self is masked_singleton: |
| 3841 | return np.asanyarray(fill_value) |
| 3842 | |
| 3843 | if m.dtype.names is not None: |
no test coverage detected