Saturday, February 21, 2026
HomeLanguagesNumpy MaskedArray.filled() method – Python

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]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS