From b4288fcea2fd5f6c1c44ae1609b3b168d273c1c5 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Sat, 15 Jun 2024 21:54:10 -0400 Subject: [PATCH] Add test for include_null for categorical data type. --- tests/unit/test_tableone.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/unit/test_tableone.py b/tests/unit/test_tableone.py index 601529b..42902a4 100644 --- a/tests/unit/test_tableone.py +++ b/tests/unit/test_tableone.py @@ -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