Skip to content

BUG: merge_asof on float does not detect float equality #59187

@RaphaelRosset

Description

@RaphaelRosset

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

left_df = pd.DataFrame({"CONTINUOUS": list(np.arange(100.0, 106.0, 0.8))})
data = [
    (98.0, 103.4, "A", "a"),
    (104.0, 106.8, "B", "b"),
]
right_df = pd.DataFrame(data, columns=["START", "END", "C1", "C2"])
output = pd.merge_asof(
    left_df.sort_values(by="CONTINUOUS"),
    right_df.sort_values(by="START"),
    left_on="CONTINUOUS",
    right_on="START",
    direction="backward",
)
print(output.to_string())

Issue Description

When merge_asof is done with On key being a float number sometimes error due to float precision issue can be seen

CONTINUOUS START END C1
0 100.0 98.0 103.4 A
1 100.8 98.0 103.4 A
2 101.6 98.0 103.4 A
3 102.4 98.0 103.4 A
4 103.2 98.0 103.4 A
5 104.0 98.0 103.4 A <=== 104.0 mapped to A is wrong
6 104.8 104.0 106.8 B
7 105.6 104.0 106.8 B

Note that changing the way CONTINOUS list of values is created makes the output as expected.
For example on my side, these 2 alternative give correct output.
[103.2, 104.0, 104.8] ==> correct output
list(np.arange(103.2, 106.0, 0.8)) ==> correct output

Expected Behavior

CONTINUOUS START END C1
0 100.0 98.0 103.4 A
1 100.8 98.0 103.4 A
2 101.6 98.0 103.4 A
3 102.4 98.0 103.4 A
4 103.2 98.0 103.4 A
5 104.0 98.0 103.4 B <=== 104.0 mapped to B is correct
6 104.8 104.0 106.8 B
7 105.6 104.0 106.8 B

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : Norwegian Bokmål_Norway.1252

pandas : 2.2.2
numpy : 2.0.0
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : None
pip : 24.1.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 : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
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 : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team memberReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions