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

polars.testing.assert_series_equal has incorrect logic for infinities #16859

Closed
2 tasks done
kevinli1993 opened this issue Jun 10, 2024 · 0 comments · Fixed by #20763
Closed
2 tasks done

polars.testing.assert_series_equal has incorrect logic for infinities #16859

kevinli1993 opened this issue Jun 10, 2024 · 0 comments · Fixed by #20763
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@kevinli1993
Copy link

kevinli1993 commented Jun 10, 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 polars as pl
import polars.testing

import numpy as np

# this passes (!)
polars.testing.assert_series_equal(pl.Series(np.random.rand(3)), pl.Series([float('inf') for _ in range(3)]), check_names=False)

Log output

No response

Issue description

When the right vector is infinity (and check_exact=False), the polars.testing.assert_series_equal(left, right) function is buggy.

This occurs because this function (inside polars/testing/asserts/series.py) uses exceeds_tolerance = difference > tolerance and in our case, both difference and tolerance are infinity.

It is arguably more correct to use exceeds_tolerance = difference >= tolerance, but of course that is not strictly backwards-compatible.

def _assert_series_values_within_tolerance(
   left: Series,
   right: Series,
   unequal: Series,
   *,
   rtol: float,
   atol: float,
) -> None:
   __tracebackhide__ = True

   left_unequal, right_unequal = left.filter(unequal), right.filter(unequal)

   difference = (left_unequal - right_unequal).abs()
   tolerance = atol + rtol * right_unequal.abs()
   exceeds_tolerance = difference > tolerance    # <----- this line

   if exceeds_tolerance.any():
       raise_assertion_error(
           "Series",
           "value mismatch",
           left.to_list(),
           right.to_list(),
       )

(I cannot use check_exact=True because in my actual use case, I have non-infinity values and I really do want check_exact=False for finite values.)

Expected behavior

The assertion should fail.

Installed versions

--------Version info---------
Polars:               0.20.31
Index type:           UInt32
Platform:             macOS-14.5-arm64-arm-64bit
Python:               3.12.3 (main, Apr 12 2024, 17:16:04) [Clang 15.0.0 (clang-1500.1.0.2.5)]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fastexcel:            <not installed>
fsspec:               <not installed>
gevent:               <not installed>
hvplot:               <not installed>
matplotlib:           3.9.0
nest_asyncio:         <not installed>
numpy:                1.26.4
openpyxl:             <not installed>
pandas:               2.2.2
pyarrow:              <not installed>
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           <not installed>
torch:                <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>
@kevinli1993 kevinli1993 added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jun 10, 2024
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

Successfully merging a pull request may close this issue.

1 participant