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

fix!: Fix NaN ordering to make NaNs compare greater than any other float, and equal to themselves #12721

Merged
merged 35 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
434a7d7
move TotalOrd to polars-utils
orlp Oct 16, 2023
a309b3b
add polars-compute
orlp Oct 16, 2023
eaed1e7
wip
orlp Oct 17, 2023
c1912d6
remove nans_compare_equal
orlp Nov 23, 2023
913a4f1
remove float comparison exceptions
orlp Nov 23, 2023
d37d9c0
mostly fix broadcasting comparisons
orlp Nov 24, 2023
01fb1bb
remove inconsistent null equality optimization
orlp Nov 27, 2023
33f09b9
add warning to always-null comparisons
orlp Nov 27, 2023
3c5e101
fmt
orlp Nov 27, 2023
14e43eb
fix warnings in tests
orlp Nov 27, 2023
5c4a943
fix _missing comparison ops
orlp Nov 27, 2023
467b7fc
clippy
orlp Nov 27, 2023
d7fb115
remove not_equal_and_validity
orlp Nov 28, 2023
5354923
add new string comparison kernels
orlp Nov 28, 2023
da58d14
define gt/ge in terms of lt/le
orlp Nov 28, 2023
74f4e3f
add _missing kernels
orlp Nov 28, 2023
b6359c4
add array support to comparison kernels
orlp Nov 28, 2023
a50997d
fmt/clippy
orlp Nov 28, 2023
5a2c321
add boolean comparison kernels
orlp Nov 29, 2023
06b9426
expand comparison tests
orlp Nov 29, 2023
80a12d9
fix test
orlp Nov 29, 2023
0b93380
user new string broadcast comparison kernels
orlp Nov 29, 2023
ed030c3
remove old comparison kernels
orlp Nov 29, 2023
ba409a4
clippy
orlp Nov 29, 2023
ea99583
fix bad/outdated tests
orlp Nov 29, 2023
9013166
fix trait bounds
orlp Nov 29, 2023
3c63fec
fix conditional import
orlp Nov 29, 2023
cb4da95
fix another bad test
orlp Nov 29, 2023
1bd4957
fix failing doctest
orlp Nov 29, 2023
ba8c5e2
address review comments
orlp Nov 30, 2023
d58dc7c
fix mypy
orlp Nov 30, 2023
e2e8b85
fix incorrect bitcount
orlp Nov 30, 2023
3b00eb1
add missing inline
orlp Nov 30, 2023
733c634
add missing comment
orlp Nov 30, 2023
7a340e4
fmt
orlp Nov 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add warning to always-null comparisons
  • Loading branch information
orlp committed Nov 27, 2023
commit 33f09b968cca77eac5361821c62f866d5ee180d5
7 changes: 7 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
from polars.utils.various import (
_prepare_row_count_args,
_process_null_values,
_warn_null_comparison,
can_create_dicts_with_pyarrow,
handle_projection_columns,
is_bool_sequence,
Expand Down Expand Up @@ -1450,21 +1451,27 @@ def __bool__(self) -> NoReturn:
)

def __eq__(self, other: Any) -> DataFrame: # type: ignore[override]
_warn_null_comparison(other)
stinodego marked this conversation as resolved.
Show resolved Hide resolved
return self._comp(other, "eq")

def __ne__(self, other: Any) -> DataFrame: # type: ignore[override]
_warn_null_comparison(other)
return self._comp(other, "neq")

def __gt__(self, other: Any) -> DataFrame:
_warn_null_comparison(other)
return self._comp(other, "gt")

def __lt__(self, other: Any) -> DataFrame:
_warn_null_comparison(other)
return self._comp(other, "lt")

def __ge__(self, other: Any) -> DataFrame:
_warn_null_comparison(other)
return self._comp(other, "gt_eq")

def __le__(self, other: Any) -> DataFrame:
_warn_null_comparison(other)
return self._comp(other, "lt_eq")

def __getstate__(self) -> list[Series]:
Expand Down
8 changes: 7 additions & 1 deletion py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
warn_closed_future_change,
)
from polars.utils.meta import threadpool_size
from polars.utils.various import no_default, sphinx_accessor
from polars.utils.various import no_default, sphinx_accessor, _warn_null_comparison

with contextlib.suppress(ImportError): # Module not available when building docs
from polars.polars import arg_where as py_arg_where
Expand Down Expand Up @@ -159,6 +159,7 @@ def __rand__(self, other: Any) -> Self:
return self._from_pyexpr(self._to_pyexpr(other)._and(self._pyexpr))

def __eq__(self, other: Any) -> Self: # type: ignore[override]
_warn_null_comparison(other)
return self._from_pyexpr(self._pyexpr.eq(self._to_pyexpr(other)))

def __floordiv__(self, other: Any) -> Self:
Expand All @@ -168,18 +169,22 @@ def __rfloordiv__(self, other: Any) -> Self:
return self._from_pyexpr(self._to_pyexpr(other) // self._pyexpr)

def __ge__(self, other: Any) -> Self:
_warn_null_comparison(other)
return self._from_pyexpr(self._pyexpr.gt_eq(self._to_pyexpr(other)))

def __gt__(self, other: Any) -> Self:
_warn_null_comparison(other)
return self._from_pyexpr(self._pyexpr.gt(self._to_pyexpr(other)))

def __invert__(self) -> Self:
return self.not_()

def __le__(self, other: Any) -> Self:
_warn_null_comparison(other)
return self._from_pyexpr(self._pyexpr.lt_eq(self._to_pyexpr(other)))

def __lt__(self, other: Any) -> Self:
_warn_null_comparison(other)
return self._from_pyexpr(self._pyexpr.lt(self._to_pyexpr(other)))

def __mod__(self, other: Any) -> Self:
Expand All @@ -195,6 +200,7 @@ def __rmul__(self, other: Any) -> Self:
return self._from_pyexpr(self._to_pyexpr(other) * self._pyexpr)

def __ne__(self, other: Any) -> Self: # type: ignore[override]
_warn_null_comparison(other)
return self._from_pyexpr(self._pyexpr.neq(self._to_pyexpr(other)))

def __neg__(self) -> Expr:
Expand Down
7 changes: 7 additions & 0 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
range_to_slice,
scale_bytes,
sphinx_accessor,
_warn_null_comparison
)

with contextlib.suppress(ImportError): # Module not available when building docs
Expand Down Expand Up @@ -559,6 +560,7 @@ def __eq__(self, other: Any) -> Series:
...

def __eq__(self, other: Any) -> Series | Expr:
_warn_null_comparison(other)
if isinstance(other, pl.Expr):
return F.lit(self).__eq__(other)
return self._comp(other, "eq")
Expand All @@ -572,6 +574,7 @@ def __ne__(self, other: Any) -> Series:
...

def __ne__(self, other: Any) -> Series | Expr:
_warn_null_comparison(other)
if isinstance(other, pl.Expr):
return F.lit(self).__ne__(other)
return self._comp(other, "neq")
Expand All @@ -585,6 +588,7 @@ def __gt__(self, other: Any) -> Series:
...

def __gt__(self, other: Any) -> Series | Expr:
_warn_null_comparison(other)
if isinstance(other, pl.Expr):
return F.lit(self).__gt__(other)
return self._comp(other, "gt")
Expand All @@ -598,6 +602,7 @@ def __lt__(self, other: Any) -> Series:
...

def __lt__(self, other: Any) -> Series | Expr:
_warn_null_comparison(other)
if isinstance(other, pl.Expr):
return F.lit(self).__lt__(other)
return self._comp(other, "lt")
Expand All @@ -611,6 +616,7 @@ def __ge__(self, other: Any) -> Series:
...

def __ge__(self, other: Any) -> Series | Expr:
_warn_null_comparison(other)
if isinstance(other, pl.Expr):
return F.lit(self).__ge__(other)
return self._comp(other, "gt_eq")
Expand All @@ -624,6 +630,7 @@ def __le__(self, other: Any) -> Series:
...

def __le__(self, other: Any) -> Series | Expr:
_warn_null_comparison(other)
if isinstance(other, pl.Expr):
return F.lit(self).__le__(other)
return self._comp(other, "lt_eq")
Expand Down
9 changes: 9 additions & 0 deletions py-polars/polars/utils/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ def is_str_sequence(
return isinstance(val, Sequence) and _is_iterable_of(val, str)


def _warn_null_comparison(obj: Any):
if obj is None:
warnings.warn(
f"comparisons with None always result in null, consider using .is_null() or .is_not_null()",
UserWarning,
stacklevel=find_stacklevel(),
)


def range_to_series(
name: str, rng: range, dtype: PolarsIntegerType | None = None
) -> pl.Series:
Expand Down