Numpy MaskedArray.filled() method – Python
numpy.MaskedArray.filled() function 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.
Syntax : numpy.MaskedArray.filled(self, fill_value = None)
Parameters :
fill_value : [scalar, optional] The value to use for invalid entries, by default is None. If None, the fill_value attribute of the array is used instead.Return :
filled_array : [ndarray] A copy of self with invalid entries replaced by fill_value or self itself as an ndarray if there are no invalid entries to be replaced.
Code #1 :
# Python program explaining# numpy.MaskedArray.filled() function # importing numpy as geek # and numpy.ma module as ma import numpy as geek import numpy.ma as ma arr = geek.ma.array([2, 4, 6, 8, 10], mask =[0, 0, 1, 0, 1], fill_value = -999)gfg = arr.filled() print(gfg) |
Output :
[ 2 4 -999 8 -999]
Code #2 :
# Python program explaining# numpy.MaskedArray.filled() function # importing numpy as geek # and numpy.ma module as ma import numpy as geek import numpy.ma as ma arr = geek.ma.array([1, 2, 3, 4, 5], mask =[1, 0, 1, 0, 0], fill_value = -999)gfg = arr.filled() print(gfg) |
Output :
[-999 2 -999 4 5]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


