tompollard / tableone

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

Allow use of groupby variable with categorical data type #64

Closed jtleider closed 6 years ago

jtleider commented 6 years ago

This is a quick fix to allow the use of a groupby variable with categorical data type, as in the following example:

import pandas as pd
from tableone import TableOne

g = pd.Series(['Domestic', 'Foreign', 'Foreign', 'Domestic', 'Domestic'],
    dtype='category')
df = pd.DataFrame({'foreign': g, 'foreign2': [0, 1, 1, 0, 0], 'mpg': [22, 27, 29, 18, 20]})
print(df)
# This works
t = TableOne(df, columns=['mpg'], groupby='foreign2')
print(t)
# This didn't work
t = TableOne(df, columns=['mpg'], groupby='foreign')
print(t)
tompollard commented 6 years ago

thanks for picking this up! i'll look into merging this shortly.

tompollard commented 6 years ago

Merged in https://github.com/tompollard/tableone/commit/66e208e1605189ae293ba5a861ab3c38e4c528b9, and fixed a few other bugs in the process. Thanks again!

tompollard commented 6 years ago

By the way, I notice that if you use print(t) rather than just t to display the table in an interpreter, you will not see the warnings in the table footer. https://github.com/tompollard/tableone/issues/66

jtleider commented 6 years ago

Awesome. Thanks for putting this package together!