Skip to content

Commit

Permalink
Fix include_null for categorical data types. Fixes #177.
Browse files Browse the repository at this point in the history
  • Loading branch information
tompollard committed Jun 16, 2024
1 parent e7ccbdb commit 51b9c56
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tableone/preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,11 @@ def handle_categorical_nulls(df: pd.DataFrame, null_value: str = 'None') -> pd.D
Returns:
- pd.DataFrame: The modified DataFrame if not inplace, otherwise None.
"""
return df.fillna(null_value)
for column in df.columns:
if df[column].isnull().any():
if df[column].dtype.name == 'category':
# Add 'None' to categories if it isn't already there
if null_value not in df[column].cat.categories:
df.loc[:, column] = df[column].cat.add_categories(null_value)
df.loc[:, column] = df[column].fillna(null_value)
return df

0 comments on commit 51b9c56

Please sign in to comment.