@ 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
🌐
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. ... Sign up for free to join this conversation on GitHub.
Author: pydantic
Discussions

numpy - how to convert float to float64 inside a list - python - Stack Overflow
i have a X_train that has 16 lists and each list has a numpy array and several other float or float64 variable. how to convert all float to float 64 inside each list? Here is my code that doesn't ... More on stackoverflow.com
🌐 stackoverflow.com
Converting numpy float64 of python float to Decimal is not the same (get rounded in numpy case)
Environment Pythonnet version: '2.5.1' Python version: 3.8.12 Operating System: Win10 .NET Runtime: 4.8.4470.0 numpy version: 1.21.4 Details Describe what you were trying to get done. I had to conv... More on github.com
🌐 github.com
June 24, 2022
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
python - Conversion from float (native float or numpy.float64) to integer not working - Stack Overflow
I have a dataframe thats loaded with floats and NaNs. I need to convert all the decimals/floats to integers. I checked that all types are numpy.float64 but my conversion to integer does not work. T... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › numpy › numpy › issues › 25836
BUG: Weird conversion behavior from np.float32 to Python float · Issue #25836 · numpy/numpy
February 16, 2024 - Because neither np.float64 or np.float32 exactly represent the decimal 5.1, some of the bits in the np.float64 representation get lost in that round trip.
🌐
GitHub
github.com › pythonnet › pythonnet › issues › 1833
Converting numpy float64 of python float to Decimal is not the same (get rounded in numpy case) · Issue #1833 · pythonnet/pythonnet
June 24, 2022 - I believe 3.0 will tell you your Decimal(np.float64) call has no matching constructor overloads forcing you to explicitly convert np.float64 to either Python float or to System.Double first. Also, to save time please add info about investigation from your side. E.g. in the example the equality of two decimals fails. What are their values at the time of comparison? Surely you checked before opening the bug. ... This is fixed in rc6. ... Sign up for free to join this conversation on GitHub.
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"])
🌐
Stack Overflow
stackoverflow.com › questions › 60823404 › conversion-from-float-native-float-or-numpy-float64-to-integer-not-working
python - Conversion from float (native float or numpy.float64) to integer not working - Stack Overflow
for i, row in df_cn.iterrows(): for col in df_cn.columns: cell = row[col] if isinstance(cell, np.float64) and np.isnan(cell) == False: cell = int(cell) df_cn.loc[i,col] = cell · This for loop does not replace the floats with ints and also I get this error even though I used df_cn.loc[]: I'm not sure what I am doing wrong, I did a similar process converting strings to datetime with similar logic and that worked out fine.
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'>
🌐
Reddit
reddit.com › r/learnpython › pandas help, cannot convert float64 value to nan?
r/learnpython on Reddit: Pandas help, cannot convert float64 value to NaN?
March 6, 2024 -

Hi,

In the example below I want to change the value in column B based on the value in column A.

Why doesn't the value change in B?

import pandas as pd
import numpy as np
data = {
    "A": [15, np.nan, None, 4],
    "B": [15, 5, 7, 4],
}
df = pd.DataFrame(data)
df = df.astype({"A": "float64", "B": "float64"})
print(df)
print(df.dtypes)
# none of these work. why?
df.loc[df['A'] == None, 'B'] = np.nan  # if col A is None then set col B to np.nan
df.loc[df['A'] == np.nan, 'B'] = np.nan  # if col A is np.nan then set col B to np.nan
df.loc[df['A'] == pd.NA, 'B'] = np.nan  # if col A is pd.NA then set col B to np.nan
df.loc[df['A'] == float("nan"), 'B'] = np.nan  # if col A is float("nan") then set col B to np.nan
print(df)

Thanks

🌐
Medium
tmlb-blog-ltd.medium.com › to-convert-a-float64-type-column-into-an-int64-or-string-type-column-in-python-short-984537c97a63
To convert a float64-type column into an int64 or string-type column in python (short) | by T Miyamoto | Medium
August 20, 2020 - But this causes error messages like TypeError: Cannot cast array from dtype(‘float64’) to dtype(‘int64’) according to the rule ‘safe'. So we now resort to the apply clause (although it can be slow): df_a['coli']= (df_a['colf'].apply(lambda w0: None if pd.isnull(w0) else np.int64(w0)) ).astype(pd.Int64Dtype()) ... Next we create a string type column based on column colf. It is well known that astype(str) converts NaN into a string ‘nan’. We try to do
🌐
Codedamn
codedamn.com › news › programming › fix-valueerror
Codedamn is Sunsetting
October 6, 2022 - Codedamn is sunsetting. Learn more about what this means for you.
🌐
GitHub
github.com › pythonnet › pythonnet › issues › 1936
failure to convert numpy.float64 into System.Double · Issue #1936 · pythonnet/pythonnet
September 12, 2022 - python code: test_array = np.array([123, 345], np.float) CSharpClass.Test(test_array) C# code public static void Test(double[] TestData) {...} I already have a fix for this issue. ... internal static bool PyFloat_Check(BorrowedReference ob) { return PyObject_TypeCheck(ob, PyFloatType); //return PyObject_TYPE(ob) == PyFloatType; // existing code } This will convert any type that is derived from PyFloatType, which includes numpy.float64
Author: pythonnet
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.

🌐
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.
🌐
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.
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]
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-float64-columns-to-int64-in-pandas
How to Convert float64 Columns to int64 in Pandas? - GeeksforGeeks
July 11, 2024 - In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4
🌐
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 - By using a simple arithmetic operation, like addition with a float64 or division by 1.0, Python coerces the integer type to a float64 automatically. This trick is both quick and readable.