🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
🌐
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.5.dev0 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.4 Manual
June 22, 2021 - ‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ‘same_value’ means any data conversions may be done, but the values must not change, including rounding of floats or overflow of ints · New in version 2.4: Support for 'same_value' was added. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - NumPy: Round up/down array elements (np.floor, np.trunc, np.ceil) In addition to explicit type conversion with astype(), implicit type conversion can occur during operations. For example, division with the / operator returns float even between integers. a_int = np.array([1, 2, 3]) a_float = np.array([1.0, 2.0, 3.0]) print((a_int / a_int).dtype) # float64 print((a_int / a_float).dtype) # float64
🌐
NumPy
numpy.org › doc › stable › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.4 Manual
>>> dt = np.dtype('uint32') # 32-bit unsigned integer >>> dt = np.dtype('float64') # 64-bit floating-point number
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.1 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
🌐
Programiz
programiz.com › python-programming › numpy › methods › astype
NumPy astype()
# casting with 'no' doesn't allow casting to any other data type array2 = array1.astype(array1.dtype, casting='no') # casting with 'equiv' allows casting to equivalent data types array3 = array1.astype('<i4', casting='equiv') #cCasting with 'safe' allows casting to safe data types preserving precision array4 = array1.astype(np.float64, casting='safe') # casting with 'same_kind' allows casting to data types of the same kind array5 = array1.astype(np.int32, casting='same_kind') # casting with 'unsafe' allows casting to any data type without checks array6 = array1.astype(str, casting='unsafe') print("Array with 'no' casting:", array2) print("Array with 'equiv' casting:", array3) print("Array with 'safe' casting:", array4) print("Array with 'same_kind' casting:", array5) print("Array with 'unsafe' casting:", array6)
🌐
NumPy
numpy.org › doc › 2.1 › user › basics.types.html
Data types — NumPy v2.1 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.5.dev0 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ‘same_value’ means any data conversions may be done, but the values must not change, including rounding of floats or overflow of ints · New in version 2.4: Support for 'same_value' was added. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
Find elsewhere
Top answer
1 of 13
573

Use val.item() to convert most NumPy values to a native Python type:

import numpy as np

# for example, numpy.float32 -> python float
val = np.float32(0)
pyval = val.item()
print(type(pyval))         # <class 'float'>

# and similar...
type(np.float64(0).item()) # <class 'float'>
type(np.uint32(0).item())  # <class 'int'>
type(np.int16(0).item())   # <class 'int'>
type(np.cfloat(0).item())  # <class 'complex'>
type(np.datetime64(0, 'D').item())  # <class 'datetime.date'>
type(np.datetime64('2001-01-01 00:00:00').item())  # <class 'datetime.datetime'>
type(np.timedelta64(0, 'D').item()) # <class 'datetime.timedelta'>
...

(A related method np.asscalar(val) was deprecated with 1.16, and removed with 1.23).


For the curious, to build a table of conversions of NumPy array scalars for your system:

for name in dir(np):
    obj = getattr(np, name)
    if hasattr(obj, 'dtype'):
        try:
            if 'time' in name:
                npn = obj(0, 'D')
            else:
                npn = obj(0)
            nat = npn.item()
            print('{0} ({1!r}) -> {2}'.format(name, npn.dtype.char, type(nat)))
        except:
            pass

There are a few NumPy types that have no native Python equivalent on some systems, including: clongdouble, clongfloat, complex192, complex256, float128, longcomplex, longdouble and longfloat. These need to be converted to their nearest NumPy equivalent before using .item().

2 of 13
45

If you want to convert (numpy.array OR numpy scalar OR native type OR numpy.darray) TO native type you can simply do :

converted_value = getattr(value, "tolist", lambda: value)()

tolist will convert your scalar or array to python native type. The default lambda function takes care of the case where value is already native.

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.astype.html
numpy.astype — NumPy v2.4 Manual
>>> import numpy as np >>> arr = np.array([1, 2, 3]); arr array([1, 2, 3]) >>> np.astype(arr, np.float64) array([1., 2., 3.])
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.astype.html
numpy.astype — NumPy v2.0 Manual
>>> arr = np.array([1, 2, 3]); arr array([1, 2, 3]) >>> np.astype(arr, np.float64) array([1., 2., 3.])
🌐
NumPy
numpy.org › doc › 2.1 › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.1 Manual
>>> dt = np.dtype('uint32') # 32-bit unsigned integer >>> dt = np.dtype('float64') # 64-bit floating-point number
🌐
NumPy
numpy.org › doc › 2.2 › user › basics.types.html
Data types — NumPy v2.2 Manual
>>> z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.0 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.
🌐
GitHub
github.com › numpy › numpy › issues › 18774
arr.astype(None) converts to float64 · Issue #18774 · numpy/numpy
April 14, 2021 - arr.astype(None) converts arr to float64. I'd expect this to either error, or do nothing. Doc string says: dtype : str or dtype Typecode or data-type to which the array is cast. Which makes it seem like it should error. import numpy as np assert ...
Author   ivirshup
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › array › astype
Python Numpy array astype() - Convert Data Type | Vultr Docs
November 8, 2024 - Recognize that astype() can optimize memory usage by converting to more appropriate data types. Convert data types to less memory-intensive types when precision is not critical. ... import numpy as np high_precision = np.array([1.123456789, 2.987654321], dtype=np.float64) low_precision = high_precision.astype(np.float32) print("High precision:", high_precision) print("Low precision:", low_precision) Explain Code
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-numpy-to-convert-array-elements-to-float-type
Using NumPy to Convert Array Elements to Float Type - GeeksforGeeks
July 15, 2025 - ... Explanation: Here, a string ... np.float64() is a NumPy universal function (ufunc) that converts the elements of an array into 64-bit floating-point numbers....
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.2 Manual
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns a newly allocated array.