Use .astype.

>>> a = numpy.array([1, 2, 3, 4], dtype=numpy.float64)
>>> a
array([ 1.,  2.,  3.,  4.])
>>> a.astype(numpy.int64)
array([1, 2, 3, 4])

See the documentation for more options.

Answer from kennytm on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
Sometimes the conversion can overflow, for instance when converting a numpy.int64 value 300 to numpy.int8. NumPy follows C casting rules, so that value would overflow and become 44 (300 - 256). If you wish to avoid such overflows, you can specify that the overflow action fail by using same_value for the casting argument (see also Overflow errors): >>> z.astype(np.float64, casting="same_value") array([0., 1., 2.])
Discussions

python - convert number from numpy.float64 to integer - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... edad id_tipo_propiedad 0 NaN 2 1 NaN 2 2 NaN 2 3 NaN 2 4 NaN 2 5 NaN 2 6 2011.0 2 7 NaN 2 8 NaN 2 9 NaN 2 · The edad column is numpy.float64 type. More on stackoverflow.com
🌐 stackoverflow.com
Converting numpy dtypes to native python types - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... If I have a numpy dtype, how do I automatically convert it to its closest python data type? For example, numpy.float32 -> "python float" numpy.float64 -> "python float" numpy.uint32 -> "python ... More on stackoverflow.com
🌐 stackoverflow.com
April 16, 2020
python - Convert numpy.float64 to integer - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Im currently working on an assignment but have encountered a problem. How do I convert a numpy.float64 to integer. More on stackoverflow.com
🌐 stackoverflow.com
August 18, 2019
'numpy.float64' object cannot be interpreted as an integer
What line is throwing the error? More on reddit.com
🌐 r/DSP
15
5
June 8, 2021
🌐
Statology
statology.org › home › how to fix: ‘numpy.float64’ object cannot be interpreted as an integer
How to Fix: 'numpy.float64' object cannot be interpreted as an integer
October 5, 2021 - By using the int() function, we convert each float value in the NumPy array to an integer so we avoid the TypeError we encountered earlier.
🌐
Medium
medium.com › @amit25173 › different-ways-to-convert-numpy-float-to-int-f47f3be42453
Different Ways to Convert NumPy Float to Int | by Amit Yadav | Medium
April 12, 2025 - The easiest and most common way to convert floats to integers in NumPy is by using .astype(int).
🌐
Statology
statology.org › home › how to convert numpy array of floats into integers
How to Convert NumPy Array of Floats into Integers
April 3, 2025 - Notice that each float has been rounded up to the nearest integer and the new array has a dtype of int64. The following code shows how to convert a NumPy array of floats to an array of integers using the np.floor() function, which rounds each value down to the nearest integer:
🌐
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.5.dev0 Manual
Sometimes the conversion can overflow, for instance when converting a numpy.int64 value 300 to numpy.int8. NumPy follows C casting rules, so that value would overflow and become 44 (300 - 256). If you wish to avoid such overflows, you can specify that the overflow action fail by using same_value for the casting argument (see also Overflow errors): >>> z.astype(np.float64, casting="same_value") array([0., 1., 2.])
🌐
NumPy
numpy.org › doc › 2.1 › user › basics.types.html
Data types — NumPy v2.1 Manual
To convert the type of an array, use the .astype() method. For example: ... 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.
🌐
Stack Overflow
stackoverflow.com › questions › 71593052 › convert-number-from-numpy-float64-to-integer
python - convert number from numpy.float64 to integer - Stack Overflow
So you can use df.edad.astype('Int64') ... it is not possible to convert from numpy.float64 to integer, first it is necessary to fill NaN values and then transform....
Find elsewhere
Top answer
1 of 13
570

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.

🌐
Stack Overflow
stackoverflow.com › questions › 57541134 › convert-numpy-float64-to-integer
python - Convert numpy.float64 to integer - Stack Overflow
August 18, 2019 - And while you're at it, you should delete int(bike_2011) and int(bike_2012) -- these do absolutely nothing. On the other hand, you program has more severe problems, count_days returns a single number, so bike_2011 and bike_2012 are a plain variables, not lists or tuples, so trying to do len(bike_2011) will give you the same error, no matter if bike_2011 is integer or float.
🌐
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-41.php
NumPy: Convert numpy dtypes to native python types - w3resource
August 28, 2025 - This problem involves writing a NumPy program to convert NumPy data types (dtypes) to native Python types. The task requires using NumPy's type conversion functions to transform NumPy-specific data types, such as numpy.int32 or numpy.float32, into their equivalent native Python types, like int or float.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.4 Manual
June 22, 2021 - To avoid this, one should use a.real.astype(t). ... Try it in your browser! >>> import numpy as np >>> x = np.array([1, 2, 2.5]) >>> x array([1. , 2. , 2.5]) ... >>> x.astype(int, casting="same_value") Traceback (most recent call last): ...
🌐
Reddit
reddit.com › r/dsp › 'numpy.float64' object cannot be interpreted as an integer
r/DSP on Reddit: 'numpy.float64' object cannot be interpreted as an integer
June 8, 2021 -

I am trying to do a spectral flux analysis of an .wav input signal and I am getting this error and I don't know how to fix it. The code is:

! The errors are fixed now and the solutions are added as comments. Thank you all for the help ^

import numpy as np

from numpy.lib import stride_tricks


def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
    from numpy.lib import stride_tricks
    win = window(frameSize)
    hopSize = int(frameSize - np.floor(overlapFac * frameSize))

    # zeros at beginning (thus center of 1st window should be for sample nr. 0)
    samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)
    #fix error:samples = np.append(np.zeros(int(np.floor(frameSize/2.0))), sig)
# cols for windowing
    #fix error:cols = int(np.ceil( (len(samples) - frameSize) /float(hopSize))+1)
    cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
    # zeros at end (thus samples can be fully covered by frames)
    samples = np.append(samples, np.zeros(frameSize))

    frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
    frames *= win

    return np.fft.rfft(frames)

spectral flux function:

def spectral_Flux(wavedata, window_size, sample_rate):
    # convert to frequency domain
    magnitude_spectrum = stft(wavedata, window_size)
    timebins, freqbins = np.shape(magnitude_spectrum)

    # when do these blocks begin (time in seconds)?
    timestamps = (np.arange(0,timebins - 1) * (timebins / float(sample_rate)))

    sf = np.sqrt(np.sum(np.diff(np.abs(magnitude_spectrum))**2, axis=1)) / freqbins

    return sf[1:] #, np.asarray(timestamps)

the Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-157-54c4d632ffbf> in <module>
----> 1 sFlux=spectral_Flux(data,512,fs)

<ipython-input-156-f14509755e4e> in spectral_Flux(wavedata, window_size, sample_rate)
      1 def spectral_Flux(wavedata, window_size, sample_rate):
      2     # convert to frequency domain
----> 3     magnitude_spectrum = stft(wavedata, window_size)
      4     timebins, freqbins = np.shape(magnitude_spectrum)
      5 

<ipython-input-155-b145f5690a25> in stft(sig, frameSize, overlapFac, window)
     11 
     12     # zeros at beginning (thus center of 1st window should be for sample nr. 0)
---> 13     samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)
     14     # cols for windowing
     15     cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1

TypeError: 'numpy.float64' object cannot be interpreted as an integer

The seccond error:

TypeError                                 Traceback (most recent call last)
<ipython-input-193-54c4d632ffbf> in <module>
----> 1 sFlux=spectral_Flux(data,512,fs)

<ipython-input-192-f14509755e4e> in spectral_Flux(wavedata, window_size, sample_rate)
      1 def spectral_Flux(wavedata, window_size, sample_rate):
      2     # convert to frequency domain
----> 3     magnitude_spectrum = stft(wavedata, window_size)
      4     timebins, freqbins = np.shape(magnitude_spectrum)
      5 

<ipython-input-191-0ee2f8ea37ed> in stft(sig, frameSize, overlapFac, window)
     17     samples = np.append(samples, np.zeros(frameSize))
     18 
---> 19     frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
     20     frames *= win
     21 

C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\stride_tricks.py in as_strided(x, shape, strides, subok, writeable)
     99         interface['strides'] = tuple(strides)
    100 
--> 101     array = np.asarray(DummyArray(interface, base=x))
    102     # The route via `__interface__` does not preserve structured
    103     # dtypes. Since dtype should remain unchanged, we set it explicitly.

C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order)
     81 
     82     """
---> 83     return array(a, dtype, copy=False, order=order)
     84 
     85 

TypeError: 'numpy.float64' object cannot be interpreted as an integer

🌐
GeeksforGeeks
geeksforgeeks.org › how-to-fix-numpy-float64-object-cannot-be-interpreted-as-an-integer
How to Fix: ‘numpy.float64’ object cannot be interpreted as an integer - GeeksforGeeks
December 19, 2021 - The error "Can't assign a numpy.ndarray to a torch.FloatTensor" occurs when you attempt to assign a NumPy array to a PyTorch tensor directly, which is not allowed. PyTorch and NumPy are different libraries with different data structures, so you need to convert between them properly. Here's how you c ... In this article we will discuss how to fix the value error - cannot convert float NaN to integer in Python.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-numpy-array-of-floats-into-integers
How to Convert NumPy Array of Floats into Integers - GeeksforGeeks
April 21, 2025 - Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us ... In Python, you can convert a float to an integer using type conversion.
🌐
w3tutorials
w3tutorials.net › blog › savetxt-how-change-the-type-from-float64-to-int-or-double
How to Change Data Type from float64 to int or double in numpy savetxt: Fixing Unwanted Float Output Format — w3tutorials.net
This blog post dives deep into solving these problems. We’ll explore how to convert NumPy array data types from `float64` to `int` or `double`, and how to fix unwanted float output formats using `savetxt()`. By the end, you’ll have the tools to save clean, well-formatted text files tailored ...
🌐
GitHub
github.com › numpy › numpy › issues › 17283
Why does int64's is not inherited from "int", but "float64" is inherited from "float"? · Issue #17283 · numpy/numpy
September 10, 2020 - import numpy numpy.float64.mro() -> [numpy.float64, numpy.floating, numpy.inexact, numpy.number, numpy.generic, # Here the standard float type: float, object] numpy.int64.mro() -> [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, object] isinstance(numpy.float64(1.2345), float) -> True isinstance(numpy.int64(1), int) -> False
Author   svetlyak40wt
🌐
AskPython
askpython.com › home › how to convert float64 columns to int64 in pandas?
How to Convert float64 Columns to int64 in Pandas? - AskPython
February 27, 2023 - It is evident from the above image that both columns selected have been converted into the int64 data type. This is an alternate technique for the conversion of the selected column(s) in a dataframe from the existing data type to another. It is to be noted that it also requires some assistance from the numpy library. So, let us import it before getting any further. ... To convert the ‘Salary’ column from float64 to int64, the following code shall be used.
🌐
GitHub
github.com › samuelcolvin › pydantic › issues › 1879
numpy.float64 is not converted to float · Issue #1879 · pydantic/pydantic
August 28, 2020 - In the code below, I would expect the numpy.float64 to be converted to a float by the Foo pydantic object. from pydantic import BaseModel import numpy as np · class Foo(BaseModel): x: float · value = np.float64(13) print(type(value)) <class 'numpy.float64'> foo = Foo(x=value) print(type(foo.x)) <class 'numpy.float64'> Note that for numpy.int64 and int this works as expected: class Bar(BaseModel): x: int ·
Author   dewolfarno