Change language

Numpy MaskedArray.astype () Function | python

numpy.MaskedArray.astype() returns a copy of the MaskedArray cast to this new type.

Syntax: numpy.MaskedArray.astype(newtype)

Parameters:
newtype: Type in which we want to convert the masked array.

Return: [MaskedArray] A copy of self cast to input newtype. The returned record shape matches self.shape.

Code # 1:

# Python program explaining
# numpy.MaskedArray.astype () method

 
# import numy as geek
# and numpy.ma module as ma

import numpy as geek

import numpy.ma as ma

 
# create input array

in_arr = geek.array ([ 1 , 2 3 , - 1 , 5 ])

print ( "Input array:" , in_arr)

 
# Now we create a masked int32 array
# and invalidate the third entry.

mask_arr = ma.masked_array (in_arr, mask = [ 0 , 0 , 1 , 0 , 0 ])

print ( "Masked array:" , mask_arr)

  
# print disguised aaray data

print (mask_arr.dtype) 

 
# applying MaskedArray.astype methods to mask the array
# and convert it to float64

out_arr = mask_arr.astype ( ’float64’ )

print ( " Output typecasted array: " , out_arr)

  
# print data type Typecasted Masque Aaray

print (out_arr.dtype) 

Output:

 Input array: [1 2 3 -1 5] Masked array: [1 2 - -1 5] int32 Output typecasted array: [1.0 2.0 - -1.0 5.0] float64 

Code # 2:

# Python program explaining
# numpy.MaskedArray.astype () method

 
# import numy as geek
# and numpy.ma module as ma

import numpy as geek

import numpy.ma as ma

 
# create input array

in_arr = geek.array ([ 10.1 , 20.2 , 30.3 , 40.4 , 50.5 ], dtype = ’float64’ )

print ( " Input array: " , in_arr)

  
# Now we create a masked array, making
# the first and third entries are invalid.

mask_arr = ma.masked_array (in_arr, mask = [ 1 , 0 , 1 , 0 , 0 ])

print ( "Masked array:" , mask_arr)

 
# print data like a disguised aaray

print (mask_arr.dtype) 

 
# using MaskedArray.astype methods to mask the array
# and convert it to int32

out_arr = mask_arr.astype ( ’int32’ )

print ( "Output typecasted array:" , out_arr)

 
# print data type Typecasted Masque Aaray

print (out_arr.dtype) 

Output :

 Input array: [10.1 20.2 30.3 40.4 50.5] Masked array: [- 20.2 - 40.4 50.5 ] float64 Output typecasted array: [- 20 - 40 50] int32 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically