Friday, November 21, 2025
HomeLanguagesNumpy ndarray.setfield() function | Python

Numpy ndarray.setfield() function | Python

numpy.ndarray.setfield() function Put a value into a specified place in a field defined by a data-type. Place val into a’s field defined by dtype and beginning offset bytes into the field.

Syntax : numpy.ndarray.setfield(val, dtype, offset=0)

Parameters :
val : [object] Value to be placed in field.
dtype : [dtype object] Data-type of the field in which to place val.
offset : [int, optional] The number of bytes into the field at which to place val.

Return : None.

Code #1 :




# Python program explaining
# numpy.ndarray.setfield() function
  
# importing numpy as geek 
import numpy as geek 
            
arr = geek.eye(4)
            
arr.setfield(4, geek.int32)
gfg = arr.getfield(geek.int32)
      
print(gfg)


Output :

[[4 4 4 4]
 [4 4 4 4]
 [4 4 4 4]
 [4 4 4 4]]

 
Code #2 :




# Python program explaining
# numpy.ndarray.setfield() function
  
# importing numpy as geek 
import numpy as geek 
            
arr = geek.eye(2)
            
arr.setfield(2, geek.int32)
gfg = arr.getfield(geek.int32)
      
print(gfg)


Output :

[[2 2]
 [2 2]]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11997 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS