1. If the dataframe (say df) wholly consists of float64 dtypes, you can do:
df = df.astype('float32')
  1. Only if some columns are float64, then you'd have to select those columns and change their dtype:
# Select columns with 'float64' dtype  
float64_cols = list(df.select_dtypes(include='float64'))

# The same code again calling the columns
df[float64_cols] = df[float64_cols].astype('float32')
Answer from Shiva Govindaswamy on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_numeric.html
pandas.to_numeric — pandas 3.0.2 documentation - PyData |
Convert dtypes. ... >>> s = pd.Series(["1.0", "2", -3]) >>> pd.to_numeric(s) 0 1.0 1 2.0 2 -3.0 dtype: float64 >>> pd.to_numeric(s, downcast="float") 0 1.0 1 2.0 2 -3.0 dtype: float32 >>> pd.to_numeric(s, downcast="signed") 0 1 1 2 2 -3 dtype: int8 >>> s = pd.Series(["apple", "1.0", "2", -3]) >>> pd.to_numeric(s, errors="coerce") 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64
🌐
Rip Tutorial
riptutorial.com › changing dtypes
pandas Tutorial => Changing dtypes
In [4]: df['A'].astype('float') Out[4]: 0 1.0 1 2.0 2 3.0 Name: A, dtype: float64 In [5]: df['B'].astype('int') Out[5]: 0 1 1 2 2 3 Name: B, dtype: int32
People also ask

How do I convert a pandas column to float?
You can convert a pandas column to float using the astype() method. Example: df['column'] = df['column'].astype(float). This changes the column data type to float.
🌐
golinuxcloud.com
golinuxcloud.com › home › python pandas › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & ...
How do I convert an object column to float in pandas?
Object columns can be converted to float using pd.to_numeric() or astype(float). Example: df['column'] = pd.to_numeric(df['column']). This safely converts string values to numeric format.
🌐
golinuxcloud.com
golinuxcloud.com › home › python pandas › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & ...
How do I convert multiple columns to float in pandas?
You can convert multiple columns by passing a dictionary to astype(). Example: df = df.astype({'col1': float, 'col2': float}).
🌐
golinuxcloud.com
golinuxcloud.com › home › python pandas › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & ...
🌐
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']) ...
🌐
GoLinuxCloud
golinuxcloud.com › home › python pandas › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & Practical Examples) | GoLinuxCloud
January 24, 2022 - import pandas as pd df = pd.DataFrame({ "price": [10, 20, 30, 40] }) df["price"] = df["price"].astype(float) print(df.dtypes) This converts the price column from integer to float64.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 55679
BUG: setting 1.02 into float32 Series upcasts to float64 · Issue #55679 · pandas-dev/pandas
October 25, 2023 - Value '1.02' has dtype incompatible with float32, please explicitly cast to a compatible dtype first. It then shows that the post-assignment dtype is float64, which is presumably why the warning is being raised: Pandas is converting the backing array from f4 to f8 behind the scenes for some reason.
Author   batterseapower
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert column to float in dataframe
Pandas Convert Column to Float in DataFrame - Spark By {Examples}
October 14, 2024 - You can use the Pandas DataFrame.astype() function to convert a column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to a 54-bit signed float, you can use numpy.float64, ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-integers-to-floats-in-pandas-dataframe
How to Convert Integers to Floats in Pandas DataFrame? | GeeksforGeeks
August 25, 2020 - # Now we will convert it from 'int' to 'float' type # using pandas.to_numeric() df['Weight'] = pd.to_numeric(df['Weight'], downcast='float') print() # lets find out the data type after changing print(df.dtypes) # print dataframe. df ...
🌐
GitHub
github.com › pandas-dev › pandas › issues › 15970
diff breaks when float64 column cast to float32 · Issue #15970 · pandas-dev/pandas
April 10, 2017 - # x is a DataFrame (x['time'].iloc[0], x['time'].dtype, np.allclose(x['time'], x['time'].astype('float32')), np.allclose(x['time'].diff(), x['time'].astype('float32').diff(), equal_nan=True)) # (1490702404.462925, dtype('float64'), True, False)
Author   jonathanstrong
🌐
w3resource
w3resource.com › python-exercises › pandas › python-pandas-data-frame-exercise-51.php
Pandas: Convert the datatype of a given column(floats to ints) - w3resource
Original DataFrame: attempts name qualify score 0 1 Anastasia yes 12.50 1 3 Dima no 9.10 2 2 Katherine yes 16.50 3 3 James no 12.77 4 2 Emily no 9.21 5 3 Michael yes 20.22 6 1 Matthew yes 14.50 7 1 Laura no 11.34 8 2 Kevin no 8.80 9 1 Jonas yes 19.13 Data types of the columns of the said DataFrame: attempts int64 name object qualify object score float64 dtype: object Now change the Data type of 'score' column from float to int: attempts name qualify score 0 1 Anastasia yes 12 1 3 Dima no 9 2 2 Katherine yes 16 3 3 James no 12 4 2 Emily no 9 5 3 Michael yes 20 6 1 Matthew yes 14 7 1 Laura no 11 8 2 Kevin no 8 9 1 Jonas yes 19 Data types of the columns of the DataFrame now: attempts int64 name object qualify object score int64 dtype: object ... Write a Pandas program to convert a column of string-encoded floats to integers and then verify the new data type.
Top answer
1 of 2
3

I think it is worth posting this as a GitHub issue. The behavior is certainly inconsistent.

The code takes a different branch based on whether the DataFrame is mixed-type or not (source).

  • In the mixed-type case the ndarray is converted to a Python list of float64 numbers and then converted back into float64 ndarray disregarding the DataFrame's dtypes information (function maybe_convert_objects()).

  • In the non-mixed-type case the DataFrame content is updated pretty much directly (source) and the DataFrame keeps its float32 dtypes.

2 of 2
2

Not an answer, but my recreation of the problem:

In [2]: df = pd.DataFrame([[1, 2, 'a'], [3, 4, 'b']], dtype=np.float32)
In [3]: df.dtypes
Out[3]: 
0    float32
1    float32
2     object
dtype: object
In [4]: A=df.ix[:,:1].values
In [5]: A
Out[5]: 
array([[ 1.,  2.],
       [ 3.,  4.]], dtype=float32)
In [6]: df.ix[:,:1] = A
In [7]: df.dtypes
Out[7]: 
0    float64
1    float64
2     object
dtype: object
In [8]: pd.__version__
Out[8]: '0.15.0'

I'm not as familiar with pandas as numpy, but I'm puzzled as to why ix[:,:1] gives me a 2 column result. In numpy that sort of indexing gives just 1 column.

If I assign a single column dtype does not change

In [47]: df.ix[:,[0]]=A[:,0]
In [48]: df.dtypes
Out[48]: 
0    float32
1    float32
2     object

The same actions without mixed datatypes does not change dtypes

In [100]: df1 = pd.DataFrame([[1, 2, 1.23], [3, 4, 3.32]], dtype=np.float32)
In [101]: A1=df1.ix[:,:1].values
In [102]: df1.ix[:,:1]=A1
In [103]: df1.dtypes
Out[103]: 
0    float32
1    float32
2    float32
dtype: object

The key must be that with mixed values, the dataframe is, in one sense or other, a dtype=object array, whether that's true of its internal data storage, or just its numpy interface.

In [104]: df1.as_matrix()
Out[104]: 
array([[ 1.        ,  2.        ,  1.23000002],
       [ 3.        ,  4.        ,  3.31999993]], dtype=float32)
In [105]: df.as_matrix()
Out[105]: 
array([[1.0, 2.0, 'a'],
       [3.0, 4.0, 'b']], dtype=object)
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'>
🌐
Skytowner
skytowner.com › explore › converting_column_type_to_float_in_pandas_dataframe
Converting column type to float in Pandas DataFrame
B float32 · dtype: object · Pandas | to_numeric method · Pandas to_numeric(~) method converts the input to a numerical type. By default, either int64 or float64 will be used. chevron_right · Pandas DataFrame | astype method · Pandas DataFrame.astype(~) method converts the data type of ...
🌐
CodeSignal
codesignal.com › learn › courses › cleaning-and-transforming-data-with-pandas › lessons › data-type-conversion-in-pandas
Data Type Conversion in Pandas
The default conversion for integers and floats in Pandas is to int64 and float64, which are 64-bit data types. These types offer higher precision and can store larger numbers compared to their 32-bit counterparts, int32 and float32. int32 and float32: These are 32-bit data types.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.astype.html
pandas.DataFrame.astype — pandas 3.0.2 documentation
Cast a pandas object to a specified dtype dtype · This method allows the conversion of the data types of pandas objects, including DataFrames and Series, to the specified dtype. It supports casting entire objects to a single data type or applying different data types to individual columns ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: How to use astype() to cast dtype of DataFrame | note.nkmk.me
August 9, 2023 - df = pd.DataFrame({'a': [11, 21, 31], 'b': [12, 22, 32], 'c': [13, 23, 33]}) print(df) # a b c # 0 11 12 13 # 1 21 22 23 # 2 31 32 33 print(df.dtypes) # a int64 # b int64 # c int64 # dtype: object df_fcol = df.astype({'a': float}) print(df_fcol) # a b c # 0 11.0 12 13 # 1 21.0 22 23 # 2 31.0 32 33 print(df_fcol.dtypes) # a float64 # b int64 # c int64 # dtype: object df_fcol2 = df.astype({'a': 'float32', 'c': 'int8'}) print(df_fcol2) # a b c # 0 11.0 12 13 # 1 21.0 22 23 # 2 31.0 32 33 print(df_fcol2.dtypes) # a float32 # b int64 # c int8 # dtype: object ... In pandas, pd.read_csv() is used to read CSV files, and you can set data types using the dtype argument.