@ DYZ's comment motivated me to solve this problem

x_train64=[]        
for (index, thing) in enumerate(X_train):
    if index < len(X_train):
        current, next_ = thing, X_train[index]
        current_list64=[np.float64(current) for current in current]
        x_train64.append(current_list64)
        if len(X_train)== index:
            print('end of X_train')
            break
Answer from Shahram on Stack Overflow
Top answer
1 of 3
32

The tolist() method should do what you want. If you have a numpy array, just call tolist():

In [17]: a
Out[17]: 
array([ 0.        ,  0.14285714,  0.28571429,  0.42857143,  0.57142857,
        0.71428571,  0.85714286,  1.        ,  1.14285714,  1.28571429,
        1.42857143,  1.57142857,  1.71428571,  1.85714286,  2.        ])

In [18]: a.dtype
Out[18]: dtype('float64')

In [19]: b = a.tolist()

In [20]: b
Out[20]: 
[0.0,
 0.14285714285714285,
 0.2857142857142857,
 0.42857142857142855,
 0.5714285714285714,
 0.7142857142857142,
 0.8571428571428571,
 1.0,
 1.1428571428571428,
 1.2857142857142856,
 1.4285714285714284,
 1.5714285714285714,
 1.7142857142857142,
 1.857142857142857,
 2.0]

In [21]: type(b)
Out[21]: list

In [22]: type(b[0])
Out[22]: float

If, in fact, you really have python list of numpy.float64 objects, then @Alexander's answer is great, or you could convert the list to an array and then use the tolist() method. E.g.

In [46]: c
Out[46]: 
[0.0,
 0.33333333333333331,
 0.66666666666666663,
 1.0,
 1.3333333333333333,
 1.6666666666666665,
 2.0]

In [47]: type(c)
Out[47]: list

In [48]: type(c[0])
Out[48]: numpy.float64

@Alexander's suggestion, a list comprehension:

In [49]: [float(v) for v in c]
Out[49]: 
[0.0,
 0.3333333333333333,
 0.6666666666666666,
 1.0,
 1.3333333333333333,
 1.6666666666666665,
 2.0]

Or, convert to an array and then use the tolist() method.

In [50]: np.array(c).tolist()
Out[50]: 
[0.0,
 0.3333333333333333,
 0.6666666666666666,
 1.0,
 1.3333333333333333,
 1.6666666666666665,
 2.0]

If you are concerned with the speed, here's a comparison. The input, x, is a python list of numpy.float64 objects:

In [8]: type(x)
Out[8]: list

In [9]: len(x)
Out[9]: 1000

In [10]: type(x[0])
Out[10]: numpy.float64

Timing for the list comprehension:

In [11]: %timeit list1 = [float(v) for v in x]
10000 loops, best of 3: 109 µs per loop

Timing for conversion to numpy array and then tolist():

In [12]: %timeit list2 = np.array(x).tolist()
10000 loops, best of 3: 70.5 µs per loop

So it is faster to convert the list to an array and then call tolist().

2 of 3
11

You could use a list comprehension:

floats = [float(np_float) for np_float in np_float_list]
Discussions

python - How to convert a numpy array from 'float64' to 'float' - Stack Overflow
How do I convert a numpy array from type 'float64' to type 'float'? Specifically, how do I convert an entire array with dtype 'float64' to have dtype 'float'? Is this possible? The answer for sc... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert a python float to a Float32?
I am trying to iterate over a Python list and compare the values to those in a Mojo Float32 list, but I have not found a way of converting a Python float to any Mojo type for comparison. I’ve tried Float32(f.to_float64(… More on forum.modular.com
🌐 forum.modular.com
5
0
February 10, 2026
pandas - How to convert datatype:object to float64 in python? - Stack Overflow
I am going around in circles and tried so many different ways so I guess my core understanding is wrong. I would be grateful for help in understanding my encoding/decoding issues. I import the dat... More on stackoverflow.com
🌐 stackoverflow.com
Converting numpy dtypes to native python types - Stack Overflow
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 -> " More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert integer to float64 in python
5 Best Ways to Convert Integer to Float64 in Python - Be on the Right Side of Change
February 18, 2024 - An example of input would be an integer 42, and the desired output is to convert this into a float64 type with the value 42.0. The float() constructor in Python can be used to cast an integer to a floating-point number, which by default is float64 ...
🌐
Python Forum
python-forum.io › thread-2181.html
pandas convert to tuple & float to float64
Dear Pandas Experts, I got two question on my Introduction to Python homework. The jupiter auto-grader expects in case 1 a float64 and in case 2 a tuple, not a list. case 1 newfour_2['GDPDiff']=np.subtract(newfour['2015'],newfour['2006']) �...
🌐
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 list ... for separate type conversion. np.float64() is a NumPy universal function (ufunc) that converts the elements of an array into 64-bit floating-point numbers....
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › how-to-convert-float64-columns-to-int64-in-pandas
How to Convert float64 Columns to int64 in Pandas? - GeeksforGeeks
July 23, 2025 - This enables the conversion of a column from various data types such as float or string to an integer type, specifically int64 or int32. We can use astype() method in Python to convert float64 Columns to int64 in Pandas.
Find elsewhere
Top answer
1 of 3
8

Yes, actually when you use Python's native float to specify the dtype for an array , numpy converts it to float64. As given in documentation -

Note that, above, we use the Python float object as a dtype. NumPy knows that int refers to np.int_, bool means np.bool_ , that float is np.float_ and complex is np.complex_. The other data-types do not have Python equivalents.

And -

float_ - Shorthand for float64.

This is why even though you use float to convert the whole array to float , it still uses np.float64.

According to the requirement from the other question , the best solution would be converting to normal float object after taking each scalar value as -

float(new_array[0])

A solution that I could think of is to create a subclass for float and use that for casting (though to me it looks bad). But I would prefer the previous solution over this if possible. Example -

In [20]: import numpy as np

In [21]: na = np.array([1., 2., 3.])

In [22]: na = np.array([1., 2., 3., np.inf, np.inf])

In [23]: type(na[-1])
Out[23]: numpy.float64

In [24]: na[-1] - na[-2]
C:\Anaconda3\Scripts\ipython-script.py:1: RuntimeWarning: invalid value encountered in double_scalars
  if __name__ == '__main__':
Out[24]: nan

In [25]: class x(float):
   ....:     pass
   ....:

In [26]: na_new = na.astype(x)


In [28]: type(na_new[-1])
Out[28]: float                           #No idea why its showing float, I would have thought it would show '__main__.x' .

In [29]: na_new[-1] - na_new[-2]
Out[29]: nan

In [30]: na_new
Out[30]: array([1.0, 2.0, 3.0, inf, inf], dtype=object)
2 of 3
3

You can create an anonymous type float like this

>>> new_array = my_array.astype(type('float', (float,), {}))
>>> type(new_array[0])
<type 'float'>
🌐
Modular
forum.modular.com › mojo
How do I convert a python float to a Float32? - Mojo - Modular
February 10, 2026 - I am trying to iterate over a Python list and compare the values to those in a Mojo Float32 list, but I have not found a way of converting a Python float to any Mojo type for comparison. I’ve tried Float32(f.to_float64()), but to_float64() returns a PythonObject, and Float32 cannot convert a PythonObject.
🌐
AskPython
askpython.com › python-modules › pandas › convert-float64-to-int64
How to Convert float64 Columns to int64 in Pandas? - AskPython
February 27, 2023 - It has a pretty straightforward syntax in which one ought to specify the column name for which the data type is to be converted which in this case would be from foat64 to int64. It is evident from the data types displayed previously that the columns ‘Work hrs’ & ‘Salary’ have the float64 data type. Since 64 seems to be the default base for both float & int data types, let us try using the below code to convert the ‘Salary’ column.
🌐
IONOS UK
ionos.co.uk › digital guide › websites › web development › converting python strings to floats
How to convert Python strings to floats - IONOS UK
January 2, 2025 - You can then use NumPy to apply the function numpy.float64() to the string string_value to convert it into a 64-bit floating number. If, on the other hand, you want to convert a float into a string, you can use the str() function.
Top answer
1 of 7
49

You can convert most of the columns by just calling convert_objects:

In [36]:

df = df.convert_objects(convert_numeric=True)
df.dtypes
Out[36]:
Date         object
WD            int64
Manpower    float64
2nd          object
CTR          object
2ndU        float64
T1            int64
T2          int64
T3           int64
T4        float64
dtype: object

For column '2nd' and 'CTR' we can call the vectorised str methods to replace the thousands separator and remove the '%' sign and then astype to convert:

In [39]:

df['2nd'] = df['2nd'].str.replace(',','').astype(int)
df['CTR'] = df['CTR'].str.replace('%','').astype(np.float64)
df.dtypes
Out[39]:
Date         object
WD            int64
Manpower    float64
2nd           int32
CTR         float64
2ndU        float64
T1            int64
T2            int64
T3            int64
T4           object
dtype: object
In [40]:

df.head()
Out[40]:
        Date  WD  Manpower   2nd   CTR  2ndU   T1    T2   T3     T4
0   2013/4/6   6       NaN  2645  5.27  0.29  407   533  454    368
1   2013/4/7   7       NaN  2118  5.89  0.31  257   659  583    369
2  2013/4/13   6       NaN  2470  5.38  0.29  354   531  473    383
3  2013/4/14   7       NaN  2033  6.77  0.37  396   748  681    458
4  2013/4/20   6       NaN  2690  5.38  0.29  361   528  541    381

Or you can do the string handling operations above without the call to astype and then call convert_objects to convert everything in one go.

UPDATE

Since version 0.17.0 convert_objects is deprecated and there isn't a top-level function to do this so you need to do:

df.apply(lambda col:pd.to_numeric(col, errors='coerce'))

See the docs and this related question: pandas: to_numeric for multiple columns

2 of 7
41

convert_objects is deprecated.

For pandas >= 0.17.0, use pd.to_numeric

df["2nd"] = pd.to_numeric(df["2nd"])
Top answer
1 of 13
574

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.

🌐
YouTube
youtube.com › codetube
Convert list of numpy float64 to float in Python quickly - YouTube
In Python, you might need to convert a list of numpy.float64 elements to regular Python float type for various purposes. This tutorial will guide you through...
Published: November 4, 2023
Views: 73
🌐
GitHub
github.com › samuelcolvin › pydantic › issues › 1879
numpy.float64 is not converted to float · Issue #1879 · pydantic/pydantic
August 28, 2020 - pydantic version: 1.6.1 pydantic compiled: True install path: /usr/local/lib/python3.7/site-packages/pydantic python version: 3.7.7 (default, Jun 9 2020, 17:58:51) [GCC 8.3.0] platform: Linux-4.19.76-linuxkit-x86_64-with-debian-10.4 optional deps. installed: ['email-validator'] In the code below, I would expect the numpy.float64 to be converted to a float by the Foo pydantic object.
Author: pydantic
🌐
Jasonblog
jasonblog.github.io › note › python › convert_list_of_numpyfloat64_to_float_and_converti.html
Convert list of numpy.float64 to float and Converting strings to floats in a DataFrame | Jason note
import pandas as pd h = pd.Series(['15', '21.0', '33.0']) l = pd.Series(['1', '2.0', '3.0']) # Converting strings to floats in a DataFrame using to_numeric h = pd.to_numeric(h) l = pd.to_numeric(l) s = h - l print type(s) print s # Convert list of numpy.float64 to float using tolist s = s.tolist() print type(s) print s
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-a-float64-column-to-int64-in-pandas
How to Convert a Float64 Column to Int64 in Pandas | Saturn Cloud Blog
May 1, 2026 - In this blog post, we'll examine the process of cleaning and manipulating data, a frequent task for data scientists and software engineers. Pandas, a widely used Python library for data analysis and manipulation, offers a range of functions to facilitate the cleaning and transformation of data.
🌐
GitHub
github.com › numpy › numpy › issues › 25836
BUG: Weird conversion behavior from np.float32 to Python float · Issue #25836 · numpy/numpy
February 16, 2024 - The biggest precision wins, so the LHS gets cast up to np.float64 before doing the comparison; you are implicitly requesting the cast up to np.float64 by using a float object on the RHS. O OTOH, if you used a np.float32 scalar on the RHS, you would have gotten the desired comparison. >>> np.array(5.1, dtype=np.float32) == np.float32(5.1) True · >>> type(np.array(5.1, dtype=np.float32).item()) float · ndarray.item() converts scalars to proper Python builtin types, in this case float (which corresponds to np.float64), so you are more or less explicitly requesting the cast up to np.float64.