Convert Boolean Columns in a Dataframe to Float
A function to convert boolean columns in a dataframe to float dtype
By Chi Kit Yeung in Python Cookbook
July 28, 2024
Problem
I ran into the error Python Numpy TypeError: ufunc 'isfinite' not supported for the input types
while trying to run statsmodel’s
variance_inflation_factor
function on a dataframe with categorical data dummy columns. The error is thrown when the data contains non-float values 1
Solution
import pandas as pd
def convert_bool_to_float(df: pd.DataFrame) -> pd.DataFrame:
"""
Converts all boolean columns in a dataframe to float type
Args:
df (pd.DataFrame): Input dataframe
Returns:
pd.DataFrame: Output dataframe with converted columns
"""
bool_cols = [col for col in df.columns if df[col].dtype == 'bool']
float_df = df.copy()
for col in bool_cols:
float_df[col] = float_df[col].astype('float')
return float_df
Examples
>>> import pandas as pd
>>> df = pd.DataFrame({"name": ['John', 'Karen', 'Stephanie'],
"single": [False, True, True],
"hungry": [True, False, True]})
>>> df
name single hungry
0 John False True
1 Karen True False
2 Stephanie True True
>>> df_float = convert_bool_to_float(df)
>>> df_float
name single hungry
0 John 0.0 1.0
1 Karen 1.0 0.0
2 Stephanie 1.0 1.0