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

Conversation

tompollard
Copy link
Owner

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)

@tompollard tompollard merged commit dd7c93d into main Jun 16, 2024
3 checks passed
@tompollard tompollard deleted the tp/issue_177 branch June 16, 2024 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant