-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: .loc slow with large DataFrame with MultiIndex while old pandas versions perform well #45681
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
Comments
Could you profile the code to check where the time is spent? The code base changed a lot since 0.25.3, so don't think we can figure anything out through the diff alone |
Looks like it is effectively all in ndarray.searchsorted within MultiIndex.get_level_indexer. Would have to look at the old code to see how that was avoided |
I don't know much about profiling so please excuse me if this is not exactly what you are looking for. I generated a call graph in PyCharm. I used the following code:
The output was
Attached is a png call graph and the pstat file generated. (had to add the .txt extension to the pstat for GitHub to let me upload it). So from what I can see, within |
this might be very similar to #38650 |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this issue exists on the latest version of pandas.
I have confirmed this issue exists on the main branch of pandas.
Reproducible Example
I am experiencing slow access with .loc on a DataFrame with ~380 million rows and a two level MultiIndex.
In this reproducible example, it takes ~2.5 seconds to do e.g.
df.loc[1000]
when having 100 million rows in the DataFrame.will print (pd.version and pd.show_versions() output omitted here)
I also ran the same code in two older versions of pandas (0.25.3. and 1.0.0). While 1.0.0 seems to be a little better than 1.4.0, the 0.25.3 version shows good performance.
I noticed that while the performance using the "base_setup" is comparable across all pandas versions, issuing a
df.loc[x]
"warm-up call" at a arbitrary position x, made pandas 0.25.3 perform thedf.loc
calls as fast asdf.iloc
(~4 orders of magnitude faster than the initialdf.loc
call), the two newer pandas versions still have painfully slowdf.loc
calls.Edit: the environments I used for testing were freshly created conda environments created with
Installed Versions
INSTALLED VERSIONS
commit : bb1f651
python : 3.10.2.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.49.1.el7.x86_64
Version : #1 SMP Tue Nov 9 16:09:48 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.utf-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.0
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 60.5.0
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
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
None
Prior Performance
pandas 0.25.3
pandas 1.0.0
The text was updated successfully, but these errors were encountered: