Friday, December 12, 2025
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
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11951 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12028 POSTS0 COMMENTS
Shaida Kate Naidoo
6947 POSTS0 COMMENTS
Ted Musemwa
7198 POSTS0 COMMENTS
Thapelo Manthata
6893 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS