Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TableOne with categorical pandas DataFrame column raises TypeError #178

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 additions & 0 deletions tests/unit/test_tableone.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,3 +1242,21 @@ def test_mutual_exclusivity_of_continuous_and_categorical(self, data_sample):

# Ensure that the error message matches the one produced by the code
assert "Columns cannot be both categorical and continuous" in str(excinfo.value)

def test_null_values_correctly_handled_for_categorical_data_type(self):
"""
Checks that null values are converted to a new category for categorical column type.

Issue raised by @eroell in https://github.com/tompollard/tableone/issues/177.
"""
dummy_table = pd.DataFrame(
{
"age": [70, 80, 90, 85, 70],
"sex": ["m", "f", "m", "f", None]
}
)
dummy_table["sex"] = dummy_table["sex"].astype("category")
t = TableOne(dummy_table, include_null=True)

expected = '1 (20.0)'
assert t.tableone.loc["sex, n (%)", "None"]["Overall"] == expected
Loading