PERF: df.unstack() is 500 times slower since pandas>=2.1 · Issue #58391 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd
import numpy as np
import time
df = pd.DataFrame(np.random.random(size=(10000, 100)))
st = time.time()
df.unstack() # this operation takes 500x more in pandas>=2.1
print(f"time {time.time() -st}")

Installed Versions

INSTALLED VERSIONS ------------------ commit : bdc79c1python : 3.11.9.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19045 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 9, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United Kingdom.1252

pandas : 2.2.1
numpy : 1.26.4
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.7
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Prior Performance

same code as above is 500x faster for pandas<=2.0.3.
Issue happens on Windows and Linux, with Python 3.10 and 3.12, with backend numpy and pyarrow.
The slow down seems to be in the stack_v3 function in the initial loop.