tompollard / tableone

Create "Table 1" for research papers in Python
https://pypi.python.org/pypi/tableone/
MIT License
159 stars 38 forks source link

TableOne with categorical pandas DataFrame column raises TypeError #178

Closed tompollard closed 1 month ago

tompollard commented 1 month ago

As highlighted in #177, setting include_null = True will fail for categorical data types (i.e. if a column has been converted to the Pandas Categorical type, include_null=True will raise an error).

After applying this fix, the following code should add the "None" category as expected:

import tableone
import pandas as pd

dummy_table = pd.DataFrame(
    {
        "age": [70, 80, 90, 85, 99],
        "sex": ["m", "f", "m", "f", None]
    }
)
dummy_table["sex"] = dummy_table["sex"].astype("category")

tableone.TableOne(dummy_table)

Returns:

Missing Overall
n 5
age, mean (SD) 0 84.8 (10.8)
sex, n (%) None 1 (20.0)
f 2 (40.0)
m 2 (40.0)