Skip to content

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

Closed
@lks100

Description

@lks100

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)                                                                                                                                                                                                                                  
Out[1]: 
col2  f  g  h  i  j
col1               
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.py, 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()

INSTALLED VERSIONS

commit: None
python: 3.7.2.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: en_US.UTF-8

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypePerformanceMemory or execution speed performance

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions