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

pivot_table very slow on Categorical data; how about an observed keyword argument? #24923

lks100 opened this issue Jan 25, 2019 · 0 comments


Copy link

commented Jan 25, 2019

Code Sample, a copy-pastable example if possible

In [1]: df = pd.DataFrame({'col1': list('abcde'), 'col2': list('fghij'), 'col3': [1, 2, 3, 4, 5]})                                                                                                                                                                                                                                 

In [1]: df.pivot_table(index='col1', values='col3', columns='col2', aggfunc=np.sum, fill_value=0)                                                                                                                                                                                                                                  
col2  f  g  h  i  j
a     1  0  0  0  0
b     0  2  0  0  0
c     0  0  3  0  0
d     0  0  0  4  0
e     0  0  0  0  5

In [2]: %timeit df.pivot_table(index='col1', values='col3', columns='col2', aggfunc=np.sum, fill_value=0)                                                                                                                                                                                                                         
5.56 ms ± 89.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [3]: df.col1 = df.col1.astype('category')                                                                                                                                                                                                                                                                                      

In [4]: df.col2 = df.col2.astype('category')                                                                                                                                                                                                                                                                                      

In [5]: %timeit df.pivot_table(index='col1', values='col3', columns='col2', aggfunc=np.sum, fill_value=0)                                                                                                                                                                                                                         
94.7 ms ± 1.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Problem description

groupby has an observed keyword argument (which was added to speed up grouping with categorical data by avoiding cartesian cross-products).

Unfortunately, pivot_table does not have such an option, and since it ultimately calls groupby with observed=False in, pivot tables ultimately can be extremely slow for categorical data. Note the simple example above, which runs almost 20 times slower for categorical data (and it would be much worse if the categories were larger).

I believe by simply adding an observed=False keyword argument to pivot_table, the code will be backwards compatible, but then categorical data will be processed efficiently.

Output of pd.show_versions()


commit: None
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 40.6.2
Cython: None
numpy: 1.15.4
scipy: None
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: 1.2.0
xlwt: None
xlsxwriter: 1.1.2
lxml: None
bs4: 4.6.3
html5lib: None
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.