Skip to content

PERF: Index.union is materializing even when it is not needed when sort=False #43885

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

Closed
2 of 3 tasks
galipremsagar opened this issue Oct 4, 2021 · 0 comments · Fixed by #47557
Closed
2 of 3 tasks
Labels
Index Related to the Index class or subclasses Performance Memory or execution speed performance setops union, intersection, difference, symmetric_difference
Milestone

Comments

@galipremsagar
Copy link

  • 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 master branch of pandas.

Reproducible Example

We are currently materializing RangeIndex which is already monotonically increasing when sort=False.

>>> import pandas as pd
>>> pd.__version__
'1.3.3'
>>> idx1 = pd.RangeIndex(0, 10)
>>> idx2 = pd.RangeIndex(0, 5)
>>> idx1.union(idx2)
RangeIndex(start=0, stop=10, step=1)
>>> idx1.union(idx2, sort=False)
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')

This behavior was introduced in this PR: #25788

From the PR description, it seems to be a conscious decision, but for performance reasons we should ideally be checking the result of the union if it is monotonically increasing/decreasing and then decide to sort or not based on sort parameter. This alleviates materialization and thus memory pressure too.

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.9.6.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 57.4.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
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Prior Performance

No response

@galipremsagar galipremsagar added Needs Triage Issue that has not been reviewed by a pandas team member Performance Memory or execution speed performance labels Oct 4, 2021
@mroeschke mroeschke added Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 13, 2021
@jreback jreback added this to the 1.5 milestone Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Index Related to the Index class or subclasses Performance Memory or execution speed performance setops union, intersection, difference, symmetric_difference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants