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

ENH: Make merge_asof preserve the index #60919

Open
3 tasks done
wahsmail opened this issue Feb 12, 2025 · 3 comments
Open
3 tasks done

ENH: Make merge_asof preserve the index #60919

wahsmail opened this issue Feb 12, 2025 · 3 comments
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement Needs Discussion Requires discussion from core team before further action Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@wahsmail
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import datetime as dt

print(pd.__version__)  # 2.2.3

left = pd.DataFrame({
    'left_date': pd.date_range(dt.date(2025, 1, 1), dt.date(2025, 1, 5)),
})

left = left.drop([0, 1])

right = pd.DataFrame({
    'right_date': pd.date_range(dt.date(2025, 1, 1), dt.date(2025, 1, 5)),
    'value': range(5)
})

merged = pd.merge_asof(left, right, left_on='left_date', right_on='right_date')

index_before = left.index.tolist()
index_after = merged.index.tolist()
print(index_before)  # [2, 3, 4] since first two dropped
print(index_after)  # should be [2, 3, 4] but is [0, 1, 2]

Issue Description

merge_asof is has the unexpected side effect of resetting a RangeIndex. If there are gaps in the RangeIndex in the left dataframe ahead of the call, there will be no gaps in the dataframe resulting from the call.

Expected Behavior

By default, I would expect the index to be untouched, or at least expect an ignore_index keyword argument to be able to control this behavior.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.9
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.3
numpy : 2.2.2
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None

@wahsmail wahsmail added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 12, 2025
@rhshadrach
Copy link
Member

Thanks for the report! I do not see any indication in the documentation that this should preserve the index, and taking a look at the implementation it seems apparent to me that it wasn't intended to. Reworking this issue as an enhancement request.

If there is some indication in the docs that it should preserve the index, please do let us know.

@rhshadrach rhshadrach added Enhancement Needs Discussion Requires discussion from core team before further action and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 12, 2025
@rhshadrach rhshadrach changed the title BUG: merge_asof effectively resets RangeIndex ENH: Make merge_asof preserve the index Feb 12, 2025
@rhshadrach rhshadrach added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Feb 12, 2025
@wahsmail
Copy link
Author

Ok agreed, its not in the documentation. I noticed that pd.merge has the same effect - the docs of which say "If joining columns on columns, the DataFrame indexes will be ignored."

Philosophically I would think that left joins preserve the shape (and index) of the left dataframe. But obviously for inner/outer joins, this becomes more ambiguous.

I'll just plan to workaround this going forward, thanks.

@rhshadrach
Copy link
Member

I do think if any change is to be made here, it should be made across merge operations to maintain consistency. This would be a rather big change, and I don't see the impact as being worth the behavior change. Marking as a closing candidate.

@rhshadrach rhshadrach added the Closing Candidate May be closeable, needs more eyeballs label Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement Needs Discussion Requires discussion from core team before further action Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants