I am working with pandas data frames that have multiple columns, each of which can be numerical, categorical or boolean. In the mwe, the categorical column is a gene, and the values in 'bool_col' are either True or False for each gene. With pandas 1.2.4, aggregating on a categorical column using the median function works as I would expect:
import pandas as pd
df = pd.DataFrame.from_dict(
{
"A1": {"gene": "A", "bool_col": True, "S2": 10},
"A2": {"gene": "A", "bool_col": True, "S2": 11},
"B1": {"gene": "B", "bool_col": False, "S2": 40},
"B2": {"gene": "B", "bool_col": False, "S2": 39},
"C1": {"gene": "C", "bool_col": True, "S2": 9},
"C2": {"gene": "C", "bool_col": True, "S2": 11},
},
orient="index"
)
df_genes = df.groupby("gene").agg("median")
As I expect, the aggregated data frame looks like this, the data type of the 'bool_col' column is still boolean and column 'S2' contains the median value of the respective category:
bool_col S2
gene
A True 10.5
B False 39.5
C True 10.0
However, after updating to pandas 1.3.4, the boolean columns are cast to the 'float64' data type during the aggregation:
bool_col S2
gene
A 1.0 10.5
B 0.0 39.5
C 1.0 10.0
One could probably change the data type of the 'bool_col' column back to boolean, but is this behavior expected with pandas 1.3.4? Maybe I am missing something, but is there a way to keep the original boolean data type? Links and hints are highly appreciated!