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

join_asof with strategy="nearest" joins large negative values incorrectly when -inf is present. #20349

Open
2 tasks done
Unigurd opened this issue Dec 18, 2024 · 1 comment
Open
2 tasks done
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@Unigurd
Copy link

Unigurd commented Dec 18, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import numpy as np
import polars as pl

pl.DataFrame({"a": [-np.inf, -(2.0**61)]}).join_asof(
    pl.DataFrame({"a": [0.0, 1.0], "b": ["lower", "higher"]}), on="a", strategy="nearest"
)


# ┌────────────┬────────┐
# │ a          ┆ b      │
# │ ---        ┆ ---    │
# │ f64        ┆ str    │
# ╞════════════╪════════╡
# │ -inf       ┆ lower  │
# │ -2.3058e18 ┆ higher │
# └────────────┴────────┘

Log output

join parallel: true
ASOF join dataframes finished

Issue description

When performing join_asof with the nearest strategy and when -inf is in the joined column in the left dataframe, values strictly smaller than -(2.0**60) don't match the nearest value in the joined column of the right dataframe.

Expected behavior

I expect -(2.0**61) to be considered nearer to 0.0 than to 1.0, and thus for the resulting dataframe to be equal to pl.DataFrame({"a": [-np.inf, -(2.0**61)], "b": "lower"})

Installed versions

--------Version info---------
Polars:              1.17.1
Index type:          UInt32
Platform:            Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.39
Python:              3.11.9 (main, Apr 27 2024, 21:16:11) [GCC 13.2.0]
LTS CPU:             False

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               <not installed>
boto3                <not installed>
cloudpickle          3.1.0
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               2024.10.0
gevent               <not installed>
google.auth          <not installed>
great_tables         <not installed>
matplotlib           <not installed>
nest_asyncio         1.6.0
numpy                2.0.1
openpyxl             3.1.5
pandas               2.2.3
pyarrow              18.1.0
pydantic             2.10.3
pyiceberg            <not installed>
sqlalchemy           2.0.36
torch                <not installed>
xlsx2csv             0.8.4
xlsxwriter           3.2.0
@Unigurd Unigurd added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Dec 18, 2024
@Unigurd
Copy link
Author

Unigurd commented Dec 18, 2024

Additionally, I just noticed that

pl.DataFrame({"a": [1.0, np.nan]}).join_asof(
    pl.DataFrame({"a": [0.0, 1.0], "b": ["lower", "higher"]}), on="a", strategy="nearest"
)

yields the dataframe

┌─────┬────────┐
│ a   ┆ b      │
│ --- ┆ ---    │
│ f64 ┆ str    │
╞═════╪════════╡
│ 1.0 ┆ higher │
│ NaN ┆ higher │
└─────┴────────┘

while removing 1.0 from the left dataframe makes the computation yield

┌─────┬───────┐
│ a   ┆ b     │
│ --- ┆ ---   │
│ f64 ┆ str   │
╞═════╪═══════╡
│ NaN ┆ lower │
└─────┴───────┘

That is, whether nan is nearest to 0.0 or 1.0 depends on other values in the left dataframe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

1 participant